58 ms·
No criticism of CLOS generic functions, but they accomplish something very different from Clojure multimethods. generic functions are intended to support object
by swannodette 17y ago
No criticism of CLOS generic functions, but they accomplish something very different from Clojure multimethods. generic functions are intended to support objects. Clojure doesn't have objects, so Clojure does not limit multimethods to the kinds of use cases that generic methods are intended to solve.
Again you can dispatch on anything. Your dispatch function can return any value (a set, a hash-map, a vector, a list, a string, a number, etc.).
(defn new-dispatch
[x y z] (meta y))
(defmulti my-multi new-dispatch)
(defmethod my-multi {:meta-key1 meta-val1}
[x y z] ...)
(defmethod my-multi {:meta-key2 meta-val2}
[x y z] ...)
This one dispatches on the metadata attached to the second argument.