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[57] 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access