Clojure协议/功能优先级

时间:2013-04-12 16:02:16

标签: clojure

使用Clojure,我们有以下内容:

(defprotocol Greeter (hello [args] "Say hello"))

(extend-protocol Greeter
   String
   (hello [this] (str "Hello " this)))

(hello "world")    ; "Hello world"

到目前为止,这么好。然后我们添加:

(defn hello [args] (str "Wassup " args "?"))

将上一个表单的输出更改为:

(hello "world")    ; "Wassup world?"

有没有办法让协议优先于该功能?

2 个答案:

答案 0 :(得分:5)

  

有没有办法让协议优先于该方法?

您无法将defndefprotocol进行网格划分。这是因为defprotocol实际上为当前命名空间中的函数生成了绑定。请注意按以下顺序运行代码时出现的警告:

user=> (defn hello [args] (str "Wassup " args "?"))
#'user/hello
user=> (defprotocol Greeter (hello [args] "Say hello"))
Warning: protocol #'user/Greeter is overwriting function hello
Greeter

Protocols documentation解释了提供默认实施的正确方法是使用Object

(defprotocol Greeter (hello [args] "Say hello"))

(extend-protocol Greeter
   Object
   (hello [this] (str "Wassup " this "?")))

(extend-protocol Greeter
   String
   (hello [this] (str "Hello " this)))

(hello "world")    ; "Hello world"

(hello 1)    ; "Wassup 1?"

答案 1 :(得分:2)

协议方法函数,因此与其他任何var一样,如果你想让它们中的两个具有相同的名称,你必须将它们放在一个单独的命名空间中。