当类实现多个接口时,Clojure协议调用的解析

时间:2012-09-22 16:58:04

标签: clojure

给定Clojure中的set,map和vector实现IPersistentCollection和IFn,Clojure如何决定使用SayHi的哪个实现:

(defprotocol SayHi
  (hi [this]))

(extend-protocol SayHi
  clojure.lang.IPersistentCollection
  (hi [_] (println "Hi from collection"))
  clojure.lang.IFn
  (hi [_] (println "Hi from Fn!"))
  clojure.lang.IPersistentSet
  (hi [_] (println "Hi from set!")))

(hi #{})
Hi from set!
(hi [])
Hi from collection

1 个答案:

答案 0 :(得分:5)

协议调度是在函数的第一个参数的类型上完成的。当多个实现与第一个参数的类型匹配时,将选择最具体的实现。这就是(hi #{})调用解析为set实现而不是集合或fn实现的原因,即使set(#{})实现了这两者。

clojure-deftype.clj中的find-protocol-impl函数似乎处理实现对象解析的协议:

(defn find-protocol-impl [protocol x]
  (if (instance? (:on-interface protocol) x)
    x
    (let [c (class x)
          impl #(get (:impls protocol) %)]
      (or (impl c)
          (and c (or (first (remove nil? (map impl (butlast (super-chain c)))))
                     (when-let [t (reduce1 pref (filter impl (disj (supers c) Object)))]
                       (impl t))
                     (impl Object)))))))