(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, and each has its (setf name-slot) function (new value first), which #'(setf name-slot), fdefinition and fboundp see

An accessor, its setf place and the copier signal a type-error when the object is not a structure instance. The report names the function and the type, and type-error-datum/type-error-expected-type answer the object and the structure name. Any structure instance passes the check, so an instance of another structure type is not rejected. On the wasm-GC backends the error is catchable only in a module compiled in exception-handling mode (a program with a catching form); elsewhere it traps, as every runtime failure there does.

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 in the package current at the defstruct, as in Common Lisp (geo::make-pt, or geo:make-pt when its defpackage exports it), even when the struct name is a symbol of another package, inherited or written package-qualified (from cl-user, (defstruct geo::pt x) defines make-pt and pt-x); a generated name that package already inherits, such as an exported make-pt of a used package, is that symbol. The prefixes and suffix are the literal MAKE-, -P and COPY-, and the struct and slot names keep their case: (defstruct |low| |x|) defines |MAKE-low|, |low-P|, |COPY-low| and |low-x|.

An instance is a first-class structure object, not a list: print shows it in the standard #S(NAME :SLOT value ...) syntax, consp/listp are nil on instances, and two distinct instances are not equal, as in Common Lisp -- equalp compares them slot by slot. The options syntax (defstruct (name option...) slot...) supports (:constructor name), (:conc-name prefix) (a string designator: symbol, string or character), (:predicate name), (:copier name), (:include parent (slot new-default) ...), (:type (vector ...)), (:type list), :named, (:print-object fn) and (:print-function fn) on every backend, and a documentation string before the slots is accepted and dropped. A BOA constructor -- (:constructor name (lambda-list)) -- is supported in a lite form: a slot named by the lambda list reads that parameter, and every other slot evaluates its initform in the constructor body. The slot option :type is parsed and ignored. A :read-only slot -- (slot default :read-only t), also in an :include slot-override, and inherited by an :include child -- has a reader and no writer: (fboundp '(setf name-slot)) is nil, and (setf (name-slot obj) v) calls that undefined function, which signals undefined-function when the form runs, as in Common Lisp (the compilers warn at compile time). The struct name is usable as a defmethod parameter specializer, and the runtime eval of a compiled program knows neither defstruct nor accessor setf places (calling the generated functions from eval works).

(:include parent) is single struct inheritance: the parent's slots come first (so its accessors, its predicate and (typep x 'parent) all work on a child instance), and the child adds its own after them. A trailing slot-override -- (:include parent (slot new-default) ...) -- re-defaults one inherited slot in THIS child's layout (the parent's own default is untouched); the slot keeps its inherited index, so the parent's accessors still read it. Overriding a slot the parent does not define is an error. (:type (vector ...)) makes the "instance" a plain vector instead of a structure object: an (unsigned-byte 8), 16 or 32 element type makes a packed integer vector and any other is dropped, accessors are aref reads and setf-able places, the copier is copy-seq, and since there is no structure tag such a type has no predicate (unless :named, below), no #S(...) syntax and cannot be a defmethod specializer (Common Lisp agrees -- a typed struct is not a structure-object). (:type list) does the same with a plain list: accessors are nth reads and setf-able places, and the copier is copy-seq. :named beside :type puts the structure name in element 0, ahead of the slots, and defines the predicate, which tests that element: (defstruct (pt (:type list) :named) x) makes (make-pt :x 1) the list (PT 1), and (pt-p '(pt 1)) is t. Without :type, :named changes nothing. A :named struct whose vector type is a packed integer element type is an error, since the name cannot be such an element. :include on a :type struct is an error.

(:print-object fn) and (:print-function fn) give the structure its own printer instead of the #S(...) syntax. fn is a function designator -- a symbol or a lambda expression; :print-object calls it with (object stream) and the older :print-function with (object stream depth), where depth is always 0 (no print level is tracked). Either one is exactly a print-object method on the structure type, so every printing operator honors it -- print, princ, prin1, format's ~A/~S -- and a later defmethod print-object on the same type replaces it. print-unreadable-object is the usual way to write the body. Giving both options is an error, and so is combining either with :type (a typed structure is a plain vector or list, with no type to dispatch on).

The same #S(NAME :SLOT value ...) syntax is also read back: a #S(...) literal in source is a self-evaluating instance, so it works quoted, inside a backquote template and inside a #(...) vector literal. The defstruct must appear in an EARLIER top-level form (the literal is built as the source is processed, just as it is in Common Lisp). Slot values are read as data and are never evaluated, so #S(BOX :V (+ 1 2)) stores the list (+ 1 2); a slot named twice keeps its leftmost value; an omitted slot takes its default, which here must be a constant rather than an expression to evaluate. A type name that is not a structure, a slot the type does not have, and an odd number of slot items are errors. The runtime read / read-from-string builds the instance too, on every backend, so (read-from-string (prin1-to-string p)) round-trips everywhere. One compiled-reader nuance: an omitted slot whose default is nil or a simple constant is honored at run time, while a default outside the re-readable constant set signals instead of substituting a wrong value (see Compiled read/load Limitations).