Defaults for multi-argument dispatch

2009-03-20 Thread Konrad Hinsen
Providing a :default implementation for multimethods is a very common and useful technique, but it is really useful only for multimethods that dispatch on a single argument. What I am looking for is an equivalent technique for multiple-argument dispatch. Suppose you have a multimethod + of

Re: Defaults for multi-argument dispatch

2009-03-20 Thread Rich Hickey
On Mar 20, 10:56 am, Konrad Hinsen konrad.hin...@laposte.net wrote: Providing a :default implementation for multimethods is a very common and useful technique, but it is really useful only for multimethods that dispatch on a single argument. I disagree about that. No dispatch value,

Re: Defaults for multi-argument dispatch

2009-03-20 Thread Paul Stadig
You could use multiple multi-methods: user= (defmulti plus-int (fn [x y] (type y))) #'user/plus-int user= (defmethod plus-int :default [x y] (println the first is an int)) #MultiFn clojure.lang.mult...@11992cc user= (defmethod plus-int java.lang.Double [x y] (println one of each)) #MultiFn

Re: Defaults for multi-argument dispatch

2009-03-20 Thread Paul Stadig
It's also not as maintainable as using a single multi-method. Like I said, not pretty, but it works. Paul On Fri, Mar 20, 2009 at 11:38 AM, David Nolen dnolen.li...@gmail.comwrote: This works well as long as you don't mind the perf hit for the second dispatch :) On Fri, Mar 20, 2009 at

Re: Defaults for multi-argument dispatch

2009-03-20 Thread Konrad Hinsen
On Mar 20, 2009, at 16:35, Paul Stadig wrote: You could use multiple multi-methods: ... Not pretty, as you said, but also not quite the same in behaviour as a single multimethod dispatching on both arguments. Multiple dispatch can be made symmetric in the arguments, whereas a chain of

Re: Defaults for multi-argument dispatch

2009-03-20 Thread Konrad Hinsen
On Mar 20, 2009, at 16:18, Rich Hickey wrote: Providing a :default implementation for multimethods is a very common and useful technique, but it is really useful only for multimethods that dispatch on a single argument. I disagree about that. No dispatch value, composite or not, is still a