這裏有一個重載的協議定義的一部分:在實現Clojure協議時,重載的方法是否可以調用它的過載對象?
(defprotocol ClientProtocol
(create-edge
[this outV label inV]
[this outV label inV data])
下面是其實現的一部分:
java.lang.RuntimeException: Unable to resolve symbol: create-edge in this context
:
(ns bulbs.neo4jserver.client
(:use [bulbs.base.client :only [ClientProtocol]]))
(deftype Neo4jClient [ns config]
ClientProtocol
(create-edge
[this outV label inV data]
(let [inV-uri (utils/normalize-uri (build-path neo4j-uri vertex-path inV))
path (build-path vertex-path, outV, "relationships")
params {:to inV-uri, :type label, :data (first data)}]
(post config path params)))
(create-edge
[this outV label inV]
;; it doesn't like this call...
(create-edge this outV label inV nil))
當第二種方法試圖調用第一它噴涌這個錯誤
當我用第一個函數對它進行編譯,然後返回並添加了第二個函數時,我在SLIME中的兩個定義都工作得更早。
但是當我將協議定義移動到一個單獨的文件並試圖重新編譯整個事情時,它會拋出一個錯誤,當第二個方法試圖調用第一個,大概是因爲第一個方法尚未定義。
Clojure的reify
文檔有此評論:
如果一個方法是在協議/接口過載,多個獨立 方法定義必須提供。
http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/reify
我想這些都不是獨立定義。
什麼是正確的方法來解決這個問題?
中,那麼是正確的 - 謝謝 - 由於協議中有許多功能和從協議定義文件到客戶端實現的一對一映射,我只是放棄':only'位,現在使用'(:use [bulbs.base.client])'。我最初的困惑是沒有意識到協議是如何工作的 - 沒有意識到協議是命名空間定義來自何處,而不是實現。 – espeed