liveBook · Manning

Struct

As explained in Clojure: The Essential Reference

A Clojure struct is a map-like data structure with a defined set of keys and structural constraints.

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

How Clojure: The Essential Reference covers it

How deep it goes
Defines the construction API and key constraints, then works through positional input, varying keys, accessors, equality, and exceptions.
Examples
Uses waypoint rows and coordinate maps to demonstrate positional construction, missing defined keys, extra keys, and accessor lookup.
What it assumes
Assumes familiarity with Clojure maps, keywords, and basic sequence operations; defines the struct-specific API explicitly.
What you can do after
Build it from scratch with create-struct or defstruct, then construct and query instances while respecting defined-key constraints.
How it connects
Connects structs to persistent maps, assoc, dissoc, accessors, and records.

Also explained in

Compare how 14 books explain struct →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.