2
我有這樣的協議命名空間中定義X:在Clojure的另一個命名空間中擴展協議?
(ns x
(:require [..]))
(defrecord MyData [something something-else])
(defprotocol MyProtocol
(my-fn [data]))
現在我想創建此協議在另一個命名空間的實現。我試着做這樣的事情:
(ns y
(:require [..])
(:import (x MyData MyProtocol)))
(extend-protocol MyProtocol
MyData
(my-fn [data]
; Do something with the data
))
然而,當我嘗試執行像這樣(從我的測試用例):
(ns y-test
(:require [x :refer [MyData my-fn]]
[...]))
...
(let [data (->MyData ...)]
(my-fn data))
我得到以下異常:
java.lang.IllegalArgumentException: No implementation of method: :my-fn of protocol: #'x/MyProtocol found for class: x.MyData
如果我將MyProtocol
移動到命名空間y
它似乎工作。我錯過了什麼?
更新
ayato_p的回答後,我所需要的協議(在Y),而不是導入,但我仍然得到同樣的錯誤。將extend-protocol
從y
改爲x
即可解決問題。
我已經試過了,但它仍然給我同樣的錯誤:/ – Johan
啊,我明白了。您還需要在'y-test'命名空間中'require'命名空間'y'。 –