On Dec 30, 11:34 am, "Marshall T. Vandegrift" <llas...@gmail.com>
wrote:
> Peter Taoussanis <ptaoussa...@gmail.com> writes:
> > Thanks- that explains it: dropping to extend works as expected.
>
> Another option I've been making use of for exactly this situation is to
> use the #= reader macro to evaluate the Class/forName at read-time.
> Something like:
>
>   (extend-protocol MyProtocol
>     java.lang.Integer (action [x] "Integer")
>     #=(Class/forName "[B") (action [x] "ByteArray"))
>
> It may not be ideal stylistically, but lets you use extend-protocol in
> such situations.

Stuff like this always worries me. It may happen to work now, but I
doubt if that's a guarantee. extend-protocol's contract is that you
give it a Symbol, which it resolves into a Class. It surely isn't
expecting a Class as an argument, because you can't enter that as a
source-code literal. If you give it a Class, it could conceivably do
something dreadful like try to call (resolve s) on it to figure out
what Class you mean, and that will fail when passed a Class.

For that matter, trying out your example, it doesn't seem to work for
me. The first fix is to use java.lang.Class instead of just Class,
since reader evaluation happens in a no-frills environment. But after
that, it seems the extend-protocol just silently does nothing:

repl-1=> (defprotocol P)
P
repl-1=> (extend-protocol P #=(java.lang.Class/forName "[B"))
nil
repl-1=> (satisfies? P (Class/forName "[B"))
false

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to