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

前端 未结 1 1511
余生分开走
余生分开走 2021-01-04 21:02

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 soluti

相关标签:
1条回答
  • 2021-01-04 21:47

    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

    0 讨论(0)
提交回复
热议问题