Chapter 18. Choosing Clojure Type Definition Forms Wisely
Clojure provides a number of different forms useful for defining types:
deftype
,defrecord
, andreify
, Clojure’s primary datatype abstractions, explored in Defining Your Own TypesMaps of all sorts, particularly useful for the most flexible de facto types, discussed in Chapter 3
proxy
andgen-class
, which focus on providing comprehensive Java and JVM interoperability, covered in Defining Classes and Implementing Interfaces
Each of these forms represents a different set of tradeoffs.
Especially when you are new to Clojure, it may be difficult to determine
when one type-definition form should be used over another. When should
you use deftype
instead of defrecord
, gen-class
instead of deftype
, or proxy
instead of reify
?
We have attempted to explore all of the nuances of these forms in the sections referenced above. However, it is sometimes helpful to have a visual reference for such things, even if it is summary in nature. With that in mind, we hope you will find the flowchart in Figure 18-1 useful. Starting with the premise that you would like to define a type in Clojure, it will guide you through the most significant points of distinction between the type definition forms in the language so that you can settle on the one that is right for your particular situation:[428]
“The ...
Get Clojure Programming 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.