> 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.
-Marshall
Am 30.12.2011 um 20:34 schrieb Marshall T. Vandegrift:
> 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.
The “right” thing is probably allowing strings as well in extend-protocol. It is allowed in several other places like gen-class or type hinting. So it seems consistent to allow also (extend-protocol "[B" …).
Sincerely
Meikel
satisfies? works on an instance:
user=> (satisfies? P (byte-array []))
true
Dave
> 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.
I'm not entirely happy with it either, but I don't think the situation
is quite that dire. The docstring for `extend-protocol' doesn't say
anything about *how* it divides up the stanzas for extended types, and
as pointed out elsewhere in this thread the implementation uses seq vs
not-seq, not symbol? vs not-symbol?. The docstrings for `extend-type'
and `extend' explicitly say that they take a "type/class" -- I'd
interpret that to mean that they can be called with anything which
resolves to a type/class via normal resolution methods, including a
literal value generated via #=.
> 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.
Apologies -- I just modified the OP's original example without thinking
it all the way through or running the code. One does need to specify
java.lang.Class/forName, which is what I've been doing in my own source.
> But after that, it seems the extend-protocol just silently does
> nothing:
(Addressed by another poster.)
-Marshall