Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: Listing all deftypes that implement some protocol within the namespace

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"
)
like image 962
ndrw Avatar asked Jan 09 '13 21:01

ndrw


People also ask

What are the different types of namespaces in Clojure?

In require, namespaces most commonly take one of three forms: clojure.set - just loads clojure.set namespace (if not already loaded) In addition to vars, Clojure also provides support for Java interop and access to Java classes, which live in packages.

What are protocols in Clojure?

Protocols were introduced in Clojure 1.2. A protocol is a named set of named methods and their signatures, defined using defprotocol: No implementations are provided Docs can be specified for the protocol and the functions The resulting functions dispatch on the type of their first argument, and thus must have at least one argument

What are the abstractions in Clojure?

In addition, Clojure supplies many implementations of these abstractions. The abstractions are specified by host interfaces, and the implementations by host classes. While this was sufficient for bootstrapping the language, it left Clojure without similar abstraction and low-level implementation facilities.

How do I declare dependencies in Clojure?

Most Clojure files represent a single namespace and declare the dependencies for that namespace at the top of the file using the ns macro, which often looks like this:


1 Answers

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))
(user.MyType)

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

like image 95
Alex Avatar answered Sep 23 '22 05:09

Alex