A Clojure struct is a map-like value with a defined set of keys that must remain present. It is useful when creating many similarly shaped values without repeating their keys at every construction site. Structs are instances of PersistentStructMap; unlike plain maps, they retain their defined keys even when no value is supplied for one. section 11.1.4
A definition supplies the key set; two constructors offer different ways to populate it:
- `(create-struct :x :y)` creates a definition; at least one key is required. `(defstruct point-2d :x :y)` assigns a definition to a var. section 11.1.4
- `(struct point-2d 1 2)` assigns values positionally, producing `{:x 1, :y 2}`. Supplying more values than defined keys throws `IllegalArgumentException`. section 11.1.4
- `struct-map` accepts key–value pairs instead: unspecified defined keys appear with `nil` values, while extra keys can also be present. Removing a defined key with `dissoc` throws `RuntimeException`. section 11.1.4
- `(accessor point-2d :x)` creates a function for retrieving that defined key from struct instances. section 11.1.4
Choose a constructor to match the input
For rows whose fields arrive in a fixed order, define `(defstruct waypoint :type :lat :lon :id)` and construct each row with `(apply struct waypoint fields)`. For maps with varying keys, use `(apply struct-map waypoint (mapcat identity coordinates))`. A coordinate map containing `:lon`, `:id`, and `:type` but no `:lat` yields a struct whose `:lat` is `nil`; an additional `:alt` key is retained. section 11.1.4
A definition is distinct from its instances: separately created definitions with the same keys do not compare equal, while structs built from them with the same values do. Adding a key with `assoc` is permitted; it does not make that key part of the original defined set. Structs retain key insertion order, but `defrecord` is generally preferred for new structured, map-like types. section 11.1.4 section 11.0













