(defprototocol P ...) does two things: define a map on the current
namespace, and generate an interface on a package that will have the same
name as the current namespace.
(defprotocol P (x [_])) ;; -> P
(class P) ;; -> clojure.lang.PersistentArrayMap
(:on-interface P) ;; -> user.P
(.isInterface user.P) ;; true
With this knowledge, we can use heuristics to roll our own version of
protocol?:
(defn protocol?
"If the argument is one of the artifacts emitted by defprotocol,
return its value (which will evaluate to logical true), else nil."
[x]
(cond
(map? x)
(if-let [i (:on-interface x)]
(and (class? i) (.isInterface i) x))
(class? x)
(let [name (.getName x)
protocol-reification-sym ;; "user.X" -> 'user/X
(as-> n name
(vec n)
(assoc n (.lastIndexOf n ".") \/)
(apply str n)
(symbol n))]
(try
(if (:on-interface (eval protocol-reification-sym))
(-> x .getName symbol eval))
(catch Exception e)))))
(do (defprotocol P)
(map protocol? [(:on-interface P) P]))
As for why is clojure.core/protocol? private, my guess is there can't be a
100% reliable way from telling Clojure-emitted maps/interfaces from regular
maps/interfaces. Or in any case, protocol? hasn't a lot of use cases.
Hope it helps!
Victor
On Friday, March 8, 2013 1:53:52 PM UTC+1, solussd wrote:
>
> It appears that, as of Clojure 1.5, clojure.core/protocol? is private.
> Does anyone know the reason for this? What's the proper way to tell if
> something is a protocol?
>
> ---
> Joseph Smith
> [email protected] <javascript:>
> @solussd
>
>
>
>
>
>
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.