I use many maps and structs in my clojure programs. What are the benefits (apart from performance) of converting these to defrecords?
Clojure allows you to create records, which are custom, maplike data types. They're maplike in that they associate keys with values, you can look up their values the same way you can with maps, and they're immutable like maps.
The generated class automatically defines all of the non-private methods of its superclasses/interfaces. This parameter can be used to specify the signatures of additional methods of the generated class.
The method bodies of reify are lexical closures, and can refer to the surrounding local scope. reify differs from proxy in that: Only protocols or interfaces are supported, no concrete superclass. The method bodies are true methods of the resulting class, not external fns.
Clojure has gen-class, reify, proxy and also deftype and defrecord to define new class-like datatypes.
I consider structs to be effectively deprecated so I don't use them at all.
When I have a fixed set of well-known keys used in many map instances, I usually create a record. The big benefits are:
Some downsides of records:
Stuart Sierra recently wrote an interesting article on "Solving the Expression Problem with Clojure 1.2", which also contains a section on defrecord
:
http://www.ibm.com/developerworks/java/library/j-clojure-protocols/index.html#datatypes
I think the whole article is a good starting point for understanding protocols and records.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With