O'Reilly logo

Clojure Programming by Brian Carper, Christophe Grand, Chas Emerick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hierarchies

Clojure’s multimethods allow us to define hierarchies to support whatever relationships your domain requires, including multiple inheritance. These hierarchies are defined in terms of relations between named objects[218] (keywords or symbols[219]) and classes.

“Hierarchies” being plural is no accident: you can have more than one hierarchy. There’s the global (and default) hierarchy and ones you can create as needed via make-hierarchy. In addition, hierarchies and multimethods are in no way restricted to a single namespace: you can extend a hierarchy (through derive) or a multimethod (via defmethod) from any namespace—not necessarily the one in which they were defined.

The global hierarchy being shared, access to it is more guarded. Namely, non-namespaced keywords (or symbols) cannot be used in the global hierarchy. This helps prevent two innocent libraries from stepping on each other’s toes by independently choosing to use the same keyword to represent different semantics.

You define a hierarchical relation with derive:

(derive ::checkbox ::checkable)     1
;= nil
(derive ::radio ::checkable)
;= nil
(derive ::checkable ::input)
;= nil
(derive ::text ::input)
;= nil
1

Recall that ::keyword is a shorthand for :current.namespace/keyword; thus, ::checkbox here is equivalent to :user/checkbox ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required