Cover by Brian Carper, Christophe Grand, Chas Emerick

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 7. Multimethods

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.

Note

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: this.

Multimethods Basics

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. ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required