4 ms·
Clojure has that. Clojure is built on abstractions. Most operations in clojure source code convert to calls to java interfaces. For example, a simple function
by arohner 16y ago
Clojure has that.
Clojure is built on abstractions. Most operations in clojure source code convert to calls to java interfaces. For example, a simple function call (map foo bar) compiles to a call to .invoke() method on map, a java object that implements clojure.lang.IFn. Similarly, all of clojure's datastructure operations boil down to calls to objects implementing the java interfaces clojure.lang.IPersistentCollection, c.l.IPersistentMap, c.l.IPersistentSet, etc.
To add a new datastructure, simply create a new class that implements the appropriate interfaces, and you'll get literal syntax support.
To answer your question, Clojure records are named classes that implement IPersistentMap so they're a drop-in replacement for "normal" maps, that also implement an additional interface so the compiler can generate java field lookups to access items without the cost of a hash lookup.
For example
(defn foo [map]
(println (:a map)))
(defrecord MyObj [a])
(foo {:a 1})
(foo (MyObj. 1))
Both calls work exactly the same, except the MyObj doesn't have hash lookups for the fields defined in the class.
- Xurinos 16y agoClojure records are named classes that implement IPersistentMap so they're a drop-in replacement for "normal" maps, that also implement an additional interface so the compiler can generate java field lookups to access items without the cost of a hash lookup. Awesome! Thank you for that. I saw people referring to it as a hash when you point out it is not, so I was confused. I sit corrected as well as happier with the Clojure design.