Defining multimethods

Defining multimethods is another way to realize polymorphism. The multimethod has a strong dispatch mechanism for polymorphism. Let's see how it works.

Getting ready

You need to add the math.numeric-tower to your project.clj file to run samples (or any other dependency management you are using):

:dependencies 
  [[org.clojure/clojure "1.8.0"] 
   [org.clojure/math.numeric-tower "0.0.4"] 
                 ] 

How to do it...

Let's see how to define and use multimethods.

Defining a multimethod

The defmulti creates new multimethods, and the defmethod creates and implements a new method of multimethod associated with a dispatch value.

The following code shows an example of the defmulti of the calculation of the volume of shapes. The first argument is the name ...

Get Clojure Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.