我有一个协议和几个在一个工作区中实现它的定义类型。如何列出实现以下协议的所有定义类型?
我已经找到了从(ns-public)过滤数据的解决方案,但我不喜欢它,因为它使用一些“魔法”来完成工作,因为我还没有找到实现目标的正确方法满足?并扩展?.
有任何想法吗?
(defprotocol Protocol
(foo[this] "just an interface method"))
(deftype Dummy [] Protocol
(foo[this] "bar"))
(defn implements? [protocol atype] "fn from clojure sources"
(and atype (.isAssignableFrom ^Class (:on-interface protocol) atype)))
(defn list-types-implementing[protocol]
(filter (fn[x] (let [[a b] x]
(when (.startsWith (str a) "->") ; dark magic
(implements? protocol
(resolve (symbol
(.replace (str a) "->" "")))))
))
(ns-publics *ns*)))
(list-types-implementing Protocol) ; => ([->Dummy #'user/->Dummy])
(let [[a b] (first(list-types-implementing Protocol))]
(foo (b)) ; => "bar"
)