On Dec 26, 10:21 pm, Peter Taoussanis <ptaoussa...@gmail.com> wrote: > Hi there, > > I'm using Clojure 1.3.0 and am running into what seems like an edge- > case problem with extend-protocol (?)... > > Given (defprotocol MyProtocol (action [x])), > > (extend-protocol MyProtocol > (Class/forName "[B") (action [x] "ByteArray") > java.lang.Integer (action [x] "Integer")) > > expands to (do (clojure.core/extend-type java.lang.Integer MyProtocol > (action [x] "Integer")) (clojure.core/extend-type (Class/forName "[B") > MyProtocol (action [x] "ByteArray"))) > > which works as expected. Note the acrobatics to get a byte[] class. > > BUT, if I reorder the implementations, > > (extend-protocol MyProtocol > java.lang.Integer (action [x] "Integer") > (Class/forName "[B") (action [x] "ByteArray")) > > the expansion becomes (do (clojure.core/extend-type java.lang.Integer > MyProtocol (action [x] "Integer") (Class/forName "[B") (action [x] > "ByteArray"))), which is malformed. > > Am I correct in assuming this isn't the desired behaviour? Thanks!
extend-protocol groups things by splitting them into seqs and symbols. (Class/forName "[B") is a seq, so it's an implementation of a protocol function, not a class. If you want to do these sorts of gymnastics you can use the underlying extend primitive directly - I believe it just takes a seq of protocol/impl-map pairs, so you get/have to do the grouping yourself. Of course, you could use extend-type instead of extend, but if you need some expression to figure out which protocol to extend you're in the same boat AFAIK, which is why I might recommend using extend. -- 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