(rontolisp) docs
← Multimethods and hierarchies

defmulti

(defmulti name docstring? dispatch-fn :default default?)

Defines a multimethod: a method table keyed by dispatch value plus a dispatcher. dispatch-fn runs on every call and its answer picks the method; the optional :default names the fallback dispatch value (:default itself without the option). :hierarchy takes a hierarchy value the dispatch search walks instead of the global one, evaluated on every dispatch. The name lowers to a rest-args function applying each call's dispatch value to the table. The dispatcher applies the dispatch function to every call argument, so a keyword dispatch reads with the second call argument as its default, like (:k m dflt) -- a keyword-dispatched multimethod takes several call arguments; set and vector dispatch values take extra arguments the same way, while class stays single-argument.

A miss with no default method signals No method in <name> for dispatch value: <value>. The dispatch search widens past exact hits through the hierarchy (see defmethod).

A defmulti of a name that already holds a multimethod changes nothing, like the oracle's: the methods, the dispatch function and the default stay. Another definition of the name in between (defn, def, ...) lets the next defmulti define again.

A class dispatch runs over the kind keywords class answers (:string, :number, :map, ...), so host spellings name the same rows: String, Number (every numeric spelling merges into :number, where the oracle tells Long from Double), dotted names like java.util.Map, clojure.lang.IPersistentVector, and nil. An Object method catches what the hierarchy search misses, ahead of the default.