プロトコルと、1 つのワークスペース内でそれを実装するいくつかの deftypes があります。次のプロトコルを実装するすべての deftypes を一覧表示するにはどうすればよいですか?
(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"
)