Chas Emerick is the founder of Snowtide, a small software company specializing in document content extraction service sand libraries for Java and .NET, and a co-author of the forthcoming "Clojure Programming" book from O'Reilly Chas is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Flowchart for choosing the right Clojure type definition form

07.23.2011
| 4211 views |
  • submit to reddit

Clojure offers a number of different forms that define types (and generating Java classes. Choosing between deftype, defrecord, reify, proxy, and gen-class can be a tripping point for those new to Clojure.

I’d obviously like to make such decisions easier for everyone.  I know that many people learn best through visual aids, so I’ve been working on a flowchart that attempts to encapsulate some of the significant choices that go into deciding between the different type-definition forms.  A draft of it is below; let me know if it is helpful to you (or not!), how you think it could be made better, etc.  I’ll update the flowchart in-place as necessary.

“The ‘Interop Zone’” demarcates use cases (e.g. needing to define multiple constructors) and forms (proxy and gen-class) that are exclusively the domain of Clojure’s JVM interoperability support. Using these forms is fine, but be aware that by doing so, you are stepping outside of Clojure’s “native” abstractions; unless you are defining a type for the express purpose of meeting interoperability requirements, one of Clojure’s simpler type-definition forms may suit your needs better.

 

From http://cemerick.com/2011/07/05/flowchart-for-choosing-the-right-clojure-type-definition-form/

Published at DZone with permission of Chas Emerick, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)