liveBook · Manning

Create-struct

As explained in Clojure: The Essential Reference

create-struct is a Clojure function that creates an anonymous struct definition from a set of keys.

create-struct creates an anonymous Clojure struct definition from a set of keys. The definition can then produce ordered, map-like instances with struct or struct-map. section 11.1.4

Listing 11.4. → Constrained Map, Guarded keys
(create-struct [& keys])
(defstruct [name & keys])
(struct-map [s & inits])
(struct [s & vals])
(accessor [s key])

The compact API listing shows how create-struct relates to defstruct, struct-map, struct, and accessor. section 11.1.4

Key behavior:

  • Requires at least one key and returns a PersistentStructMap$Def definition object. section 11.1.4
  • Pass the definition and positional values to struct; missing values become nil, while excess values throw IllegalArgumentException. section 11.1.4
  • Use struct-map for key-value initialization; defined keys default to nil, and additional keys are accepted. section 11.1.4

Parsing positional data

(let [sdef (create-struct :type :lat :lon :id)] (apply struct sdef ["VHF" "0.000000" "0.000000" "ABI"])) ;; {:type "VHF", :lat "0.000000", :lon "0.000000", :id "ABI"}

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, defaults, equality, and exceptions, but defers implementation internals beyond the returned definition type.
Examples
Uses waypoint parsing, positional rows, equality checks, and accessor-based identifier extraction.
What it assumes
Assumes basic Clojure maps and sequences while defining the struct contract and initialization behavior explicitly.
What you can do after
Build it from scratch: define keyed data shapes and construct instances positionally or through struct-map.
How it connects
Connects create-struct with defstruct, struct, struct-map, accessor, maps, and defrecord.

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.