Clojure Tutorial: record

By Xah Lee. Date:

This page is WORK IN PROGRESS.

Clojure Records

Clojure lets you define a datatype called “records”. It is like a set of key/value pairs, with predefined names of the keys.

;; defines a “record”, named “xx”, with 2 fileds, named “n1” and “n2”
(defrecord xx [n1 n2])

“Clojure records” is like a Java class, but with member variables but no methods. It is used as a data type of a predefined shape. The variables are fields.


;; define a record
(defrecord rr [n1 n2])

;; actually create one. (a instance)
(def xx (rr. 3 4))

;; access a field
(:n1 xx) ; returns 3