On Mon, Mar 22, 2010 at 7:20 PM, Rich Hickey <richhic...@gmail.com> wrote: > > On Mar 20, 2010, at 3:56 AM, Stuart Halloway wrote: > >> The questions below refer to the gist at >> https://gist.github.com/336674/9ab832a86d203731c6379404d20afded79fe5f5b and >> to protocols in general: >> >> (1) Clojure automatically types hints the first argument when extending a >> protocol to an interface or class, which is great. But you cannot override >> this with your own type hint. Wanting to do this would be very unusual, but >> see the RandomAccess/List case in the example where one interface signifies >> the performance characteristics but a different interface has the methods >> you want. Should it be possible to override the type hint? > > No. Your wanting to do this is a hint that extending the protocol to > RandomAccess is not quite right. > >> >> (2) The code for chop is repeated across multiple classes. What is the >> idiomatic way to DRY this? Should I drop down to using raw extend with maps, >> or is there more mixin support to come? >> > > The idiomatic way is to write an ordinary function, e.g. chop is not > primitive and can just be written in terms of the protocol.
Even if the functions can be written in terms of the core protocol, putting them in the protocol makes it possible to implement higher-performance specialized versions for some containers while supplying default implementations in terms of the core protocol functions via mixins. This is how Scala's Seq trait is designed: http://www.scala-lang.org/docu/files/api/scala/Seq.html -Per -- 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 To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.