(rontolisp) docs
← Special Forms

defstruct

(defstruct name slot...)

Defines a structure type named name and returns the name symbol. Each slot is either a symbol or (slot-name default), where default is evaluated at construction time when the slot is not supplied (and may refer to variables in scope). The form generates ordinary functions:

  • make-name (&key slot...) — the constructor; slots are supplied as keyword arguments, an unknown keyword is an error
  • name-p (object) — the type predicate, t for instances of this structure only
  • copy-name (object) — a shallow copier
  • name-slot (object) — one accessor per slot; accessors are also setf-able places, so setf/incf/push on (name-slot obj) work

Because the generated names are plain functions they are first-class (#'point-x, mapcar, funcall). On the compilation path defstruct is only supported as a top-level form; the interpreter also accepts it in the REPL and via load. Under a user-defined package the generated names are interned as internal symbols of that package (geo::make-pt); listing them in a defpackage :export clause is not supported.

An instance is represented as a tagged list, so print shows that representation (not the standard #S(...) syntax, which is also not read), consp/listp are t on instances, and equal compares instances slot-wise. The defstruct options syntax ((defstruct (name (:conc-name ...) ...) ...)), :include inheritance, and BOA constructors are not supported, and the runtime eval of a compiled program knows neither defstruct nor accessor setf places (calling the generated functions from eval works).