concept multimethod in category clojure

appears as: multimethods, multimethod
Clojure in Action, Second Edition

This is an excerpt from Manning's book Clojure in Action, Second Edition.

This trivial example shows how closures are easy to create and use. A closure is an important construct in Clojure (it’s no coincidence that the name of the language sounds like the word!). It can be used for information hiding (encapsulation) because nothing from outside the closure can touch the closed-over variables. Because Clojure data structures are immutable (reducing the need to make things private), macros, closures, and multimethods allow for powerful paradigms in which to create programs. It makes traditional object-oriented ideas (a la Java or C++) feel rather confining. You’ll learn about multimethods in chapter 4 and more about macros in chapter 7. We’ll also look at closures a lot more in chapter 8, which takes a deeper look at functional programming concepts.

Another related gotcha is that a redef-ed multimethod will lose all of its defmethod implementations. This is because multimethods are a rare example of mutation in Clojure: each defmethod is actually mutating the original object created by defmulti by adding new items to its dispatch table. (This is why defmulti uses defonce in the first place: so mutations to its dispatch table aren’t lost by accidental redefinition.) By creating a new multimethod all the old methods were cleared out. You can see and manipulate this dispatch table using methods and remove-method, which we’ll discuss shortly.

defmethod will create a normal function from its function body and mutate the original defmulti object to add this function to its dispatch map under the dispatch-value key. You can inspect the dispatch map of a multimethod using the functions get-method and methods. get-methods takes a multimethod and a dispatch value and returns an implementation function for that value using the same dispatch logic as calling the multimethod directly. methods just returns the entire dispatch map for a multimethod. Let’s see an example using the affiliate-fee multimethod from earlier:

You can also remove multimethod implementations using remove-method and remove-all-methods. remove-method removes the implementation for a dispatch value. Note that unlike get-method, the dispatch value to remove must be an exact match. Finally, remove-all-methods removes all implementations of a multimethod.

We’ve covered the basics of multimethods for ad hoc polymorphism using a single dispatch value to select a matching implementation. Now you’re going to see how multimethods can consider multiple dispatch values to select an implementation.

Listing 9.2. Implementing modus operandi on top of multimethods
(ns clj-in-act.ch9.modus-operandi) (defn dispatch-fn-for [method-args] `(fn ~method-args (class ~(first method-args)))) (defn expand-spec [[method-name method-args]] `(defmulti ~method-name ~(dispatch-fn-for method-args))) (defn mo-method-info [[name args]] {(keyword name) {:args `(quote ~args)}}) (defn mo-methods-registration [specs] (apply merge (map mo-method-info specs))) (defmacro def-modus-operandi [mo-name & specs] `(do (def ~mo-name ~(mo-methods-registration specs)) ~@(map expand-spec specs))) (defn expand-method [mo-name data-type [method-name & body]] `(do (alter-var-root (var ~mo-name) update-in [(keyword '~method-name) :implementors] conj ~data-type) (defmethod ~method-name ~data-type ~@body))) (defmacro detail-modus-operandi [mo-name data-type & fns] `(do ~@(map #(expand-method mo-name data-type %) fns)))