We have previously talked about protocols: they introduce a common but limited form of polymorphic dispatch—namely type-based single dispatch. In this chapter, we’ll explore multimethods, which expand dispatch flexibility to not only offer multiple dispatch, but even dispatch based on things other than argument type. That is, for a given multimethod, the implementation used for any given invocation can be chosen as a function of any property of the arguments, without one of them being privileged. Additionally, multimethods support arbitrary hierarchies and means of disambiguating multiple inheritance.
In Java, one method name can have several signatures of the same
length differing only by the types of the arguments, a situation called
overloading. However this does not constitute a kind of multiple
dispatch: the right signature is selected during compilation based on the
types of the method’s arguments. The only dynamic
dispatch occurs on the type of the privileged argument:
A multimethod is created using a
defmulti form, and implementations of a
multimethod are provided by
defmethod forms. The mnemonic is that they come in the same order as
in the word multimethod itself: first you define the
multiple dispatch then the
methods to which calls are dispatched.
Let’s look at an example: a function that fills XML/HTML nodes and
whose behavior depends on the tag name, using the representation of XML
defined by the
clojure.xml namespace. ...