Creating Ad Hoc Taxonomies

Multimethods let you create ad hoc taxonomies, which can be helpful when you discover type relationships that are not explicitly declared as such.

For example, consider a financial application that deals with checking and savings accounts. Define a Clojure struct for an account, using a tag to distinguish the two. Place the code in the namespace examples.multimethods.account. To do this, you will need to create a file named examples/multimethods/account.clj on your classpath[59] and then enter the following code:

src/examples/multimethods/account.clj
 ​(​ns​ examples.multimethods.account)​
 ​​
 ​(​defstruct​ account :id :tag :balance)​

Now, you are going to create two different checking accounts, tagged as ::Checking ...

Get Programming Clojure, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.