| I've implemented simple multimethods for Arc in lib/multi.arc on anarki. They are modeled, not after CLOS's type-based multimethods, but after Clojure's much simpler model (see http://clojure.org/multimethods). Basically, each multimethod is associated with a dispatcher function, which is applied to the arguments of the function and returns a key into a table of methods. (Although one could conceivably incorporate the types of all the arguments into this key and have type-based dispatch on all arguments, only exact type matches would work, so it wouldn't be very useful.) So, borrowing examples from the Clojure site, you can do the following: (multi area [_ 'shape])
(method area 'rect (r) (* r!height r!width))
(method area 'circle (c) (* 3.14159 (* c!radius c!radius)))
(area (obj shape 'rect height 2 width 3))
=> 6
(area (obj shape 'circle radius 1))
=> 3.14159
Since dispatching on the value of a certain key in the first argument is probably the common use-case, I've also included a macro 'multi-keyed for just that purpose. The first line of the above could be rewritten: (multi-keyed area 'shape)
You can also supply a default method to 'multi and 'multi-keyed, which is called if the key which the dispatcher returns is not in the method table. |