On Dec 1, 9:56 pm, Rich Hickey <richhic...@gmail.com> wrote: > > There are 2 ways to make a deftype reach a protocol. First, you can > implement the protocol directly in the deftype/reify, supplying the > protocol where you do interfaces, and the methods of the protocol as > methods of the type. The type will be made to implement the protocol's > interface. The second way, for types you don't control, is to use > extend-type/class/protocol, which will create method maps and register > them with the protocol. >
For the record; this means that you cannot implement two protocols directly in deftype or reify if those protocols have fns with the same names and signatures. You can, however, implement one protocol directly, and then extend it to the other, as with the following, rather silly, example: user> (ns foo) nil foo> (defprotocol Foo (write [this])) Foo foo> (ns bar) nil bar> (defprotocol Bar (write [this badger])) Bar bar> (ns me) nil me> (defprotocol Me (write [this] [this badger])) Me me> (ns user) nil user> (deftype FooMe [a b c d] foo/Foo (write [] a) bar/Bar (write [badger] b)) #'user/FooMe user> (def fm (FooMe :foo :bar :me1 :me2)) #'user/fm user> (foo/write fm) :foo user> (bar/write fm 1) :bar user> (extend-type ::FooMe me/Me (write ([this] (:c this)) ([this badger] (:d this)))) nil user> (me/write fm) :me1 user> (me/write fm 1) :me2 user> (foo/write fm) :foo user> (bar/write fm 1) :bar user> which was unexpectedly sweet, yet totally consistent with your explanation of how it works. Rich, you _are_ the Badgers Nadgers. -Dave -- 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