Take the 2-minute tour ×

I have a protocol and several deftypes implementing it within one workspace. How can I list all deftypes that implement following protocol?

I've came to the solution that filters data from (ns-public), but I don't like it, because it uses some "magic" to get the work done, as I haven't found proper way to achieve my goal with satisfies? and extends?.

Any ideas?

(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"
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

In general, this is going to be a hairy problem to solve because there are two different ways a type can satisfy a protocol. You can extend any existing Java class to a protocol using the extend-type and extend-protocol functions (this is a very powerful feature because it allows you to extend your code to work with built-in Java or Clojure types, or other third-party types that you don't control). Or, you can specify a protocol implementation directly as part of a type definition in deftype or defrecord. These two mechanisms are implemented differently.

The first case (extension via extend-type or extend-protocol) is going to be easier for you to solve because the type being extended is going to be attached to the protocol itself (a protocol essentially amounts to a generated Java interface plus a Clojure map with metadata about the protocol). You can find the types that extend the protocol by looking at the :impls key in the protocol map:

user=> (defprotocol MyProtocol (foo [this] "Protocol function"))
user=> (deftype MyType [])
user=> (extend-type MyType MyProtocol (foo [_] "hello foo!"))
user=> (keys (:impls MyProtocol))

The second case (directly implementing a protocol via deftype or defrecord) is more difficult because what's happening is the Java class generated for the type or record will directly implement the Java interface defined by the protocol (you can implement any Java interface in a deftype or defrecord, not just a protocol). Any approach to find types that extend a protocol in this manner is going to require some deal of scanning and reflection. Essentially what you're asking is, "how can I find all the Java classes implementing a given interface?"

In a typical Java application, you might do something along the lines of scanning the directories of the classpath for .class files and introspecting on them, but this won't work in Clojure because there very well might not be .class files for your types (the bytecode is generated dynamically). If you're not satisfied with your own implementation, you might check out the answer in this question and see if it's more to your liking:

Find Java classes implementing an interface

share|improve this answer
Thanks, then I'll be to using extend-type explicitly. –  ndrw Jan 11 '13 at 9:59
I've tried your solution, but I can't find a way to create deftype instance — (find-protocol-impl MyProtocol (first (keys (:impls MyProtocol)))) doesn't seem to work. Also I've tried the following(osdir.com/ml/clojure/2010-03/msg01314.html) — still no result :( –  ndrw Jan 11 '13 at 20:51
after playing with macoexpand find out that ((resolve (symbol "->MyType"))) works. At last –  ndrw Jan 11 '13 at 21:11
add comment

Your Answer


