\label Syntax::
\DefmacWithValuesNewline defstruct {name-and-options \brac{documentation} \stardown{slot-description}} {structure-name}
\auxbnf{name-and-options}{structure-name | \paren{structure-name \interleave{\down{options}}}} \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \auxbnf{options}{\down{conc-name-option} |\CR \stardown{constructor-option} |\CR \down{copier-option} |\CR \down{include-option} |\CR \down{initial-offset-option} |\CR \down{named-option} |\CR \down{predicate-option} |\CR \down{printer-option} |\CR \down{type-option}} \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \auxbnf{conc-name-option}{\kwd{conc-name} | \paren{\kwd{conc-name}} | \paren{\kwd{conc-name} \param{conc-name}}} \auxbnf{constructor-option}{\kwd{constructor} |\CR \paren{\kwd{constructor}} |\CR \paren{\kwd{constructor} \param{constructor-name}} |\CR \paren{\kwd{constructor} \param{constructor-name} \param{constructor-arglist}}} \auxbnf{copier-option}{\kwd{copier} | \paren{\kwd{copier}} | \paren{\kwd{copier} \param{copier-name}}} \auxbnf{predicate-option}{\kwd{predicate} | \paren{\kwd{predicate}} | \paren{\kwd{predicate} \param{predicate-name}}} \auxbnf{include-option}{\paren{\kwd{include} \param{included-structure-name} \stardown{slot-description}}} \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \auxbnf{printer-option}{\down{print-object-option} | \down{print-function-option}} \auxbnf{print-object-option}{\paren{\kwd{print-object} \param{printer-name}} | \paren{\kwd{print-object}}} \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \auxbnf{print-function-option}{\paren{\kwd{print-function} \param{printer-name}} | \paren{\kwd{print-function}}} \auxbnf{type-option}{\paren{\kwd{type} \param{type}}} \auxbnf{named-option}{\kwd{named}} \auxbnf{initial-offset-option}{\paren{\kwd{initial-offset} \param{initial-offset}}} \auxbnf{slot-description}{\param{slot-name} | \CR \paren{\param{slot-name} \brac{\param{slot-initform} \interleave{\down{slot-option}}}}} \auxbnf{slot-option}{\kwd{type} \param{slot-type} | \CR \kwd{read-only} \param{slot-read-only-p}}
\label Arguments and Values::
\param{conc-name}---a \symbolnamedesignator.
\param{constructor-arglist}---a boa lambda list.
\param{constructor-name}---a symbol.
\param{copier-name}---a symbol.
\param{included-structure-name}---an already-defined structure name. \issue{DEFSTRUCT-INCLUDE-DEFTYPE:EXPLICITLY-UNDEFINED} Note that a derived type is not permissible, even if it would expand into a structure name. \endissue{DEFSTRUCT-INCLUDE-DEFTYPE:EXPLICITLY-UNDEFINED}
\param{initial-offset}---a non-negative integer.
\param{predicate-name}---a symbol.
\issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \param{printer-name}---a function name or a lambda expression. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
\param{slot-name}---a symbol.
\param{slot-initform}---a form.
\param{slot-read-only-p}---a generalized boolean.
\param{structure-name}---a symbol.
\param{type}---one of the type specifiers \typeref{list}, \typeref{vector}, or \f{(vector \param{size})}, or some other type specifier defined by the implementation to be appropriate.
\param{documentation}---a string; \noeval.
\label Description::
\macref{defstruct} defines a structured type, named \param{structure-type}, with named slots as specified by the \param{slot-options}.
\macref{defstruct} defines readers for the slots and arranges for \macref{setf} to work properly on such reader functions. Also, unless overridden, it defines a predicate named \f{\param{name}-p}, defines a constructor function named \f{make-\param{constructor-name}}, and defines a copier function named \f{copy-\param{constructor-name}}. All names of automatically created functions might automatically be declared \declref{inline} (at the discretion of the implementation).
If \param{documentation} is supplied, it is attached to \param{structure-name} as a documentation string of kind \misc{structure}, \issue{DOCUMENTATION-FUNCTION-BUGS:FIX} and unless \kwd{type} is used, the \param{documentation} is also attached to \param{structure-name} as a documentation string of kind \misc{type} and as a documentation string to the class object for the class named \param{structure-name}. \endissue{DOCUMENTATION-FUNCTION-BUGS:FIX}
\macref{defstruct} defines a constructor function that is used to create instances of the structure created by \macref{defstruct}. The default name is \f{make-\param{structure-name}}. A different name can be supplied by giving the name as the argument to the \param{constructor} option. \nil\ indicates that no constructor function will be created.
After a new structure type has been defined, instances of that type normally can be created by using the constructor function for the type. A call to a constructor function is of the following form:
\Vskip 1pc! \settabs\+\hskip\leftskip&(&\cr \+&(&{\tt constructor-function-name}\cr \+&&{\tt slot-keyword-1 form-1}\cr \+&&{\tt slot-keyword-2 form-2}\cr \+&&$\ldots$)\cr \Vskip 1pc! \rm The arguments to the constructor function are all keyword arguments. Each slot keyword argument must be a keyword whose name corresponds to the name of a structure slot. All the \param{keywords} and \param{forms} are evaluated. If a slot is not initialized in this way, it is initialized by evaluating \param{slot-initform} in the slot description \issue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED} at the time the constructor function is called. \endissue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED} \issue{UNITIALIZED-ELEMENTS:CONSEQUENCES-UNDEFINED} If no \param{slot-initform} is supplied, the consequences are undefined if an attempt is later made to read the slot's value before a value is explicitly assigned. \endissue{UNITIALIZED-ELEMENTS:CONSEQUENCES-UNDEFINED}
Each \param{slot-initform} supplied for a \macref{defstruct} component, when used by the constructor function for an otherwise unsupplied component, is re-evaluated on every call to the constructor function. \issue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED} The \param{slot-initform} is not evaluated unless it is needed in the creation of a particular structure instance. If it is never needed, there can be no type-mismatch error, even if the type of the slot is specified; no warning should be issued in this case. \endissue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED} For example, in the following sequence, only the last call is an error.
\code (defstruct person (name 007 :type string)) (make-person :name "James") (make-person) \endcode
It is as if the \param{slot-initforms} were used as initialization forms for the keyword parameters of the constructor function.
\issue{DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:NOT-BOUND} The symbols which name the slots must not be used by the implementation as the names for the lambda variables in the constructor function, since one or more of those symbols might have been proclaimed \declref{special} or might be defined as the name of a constant variable. The slot default init forms are evaluated in the lexical environment in which the \macref{defstruct} form itself appears and in the dynamic environment in which the call to the constructor function appears. \endissue{DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:NOT-BOUND}
For example, if the form \f{(gensym)} were used as an initialization form, either in the constructor-function call or as the default initialization form in \macref{defstruct}, then every call to the constructor function would call gensym once to generate a new symbol.
\issue{DEFSTRUCT-SLOTS-CONSTRAINTS-NUMBER} Each \param{slot-description} in \macref{defstruct} can specify zero or more \param{slot-options}. \endissue{DEFSTRUCT-SLOTS-CONSTRAINTS-NUMBER} A \param{slot-option} consists of a pair of a keyword and a value (which is not a form to be evaluated, but the value itself). For example:
\code (defstruct ship (x-position 0.0 :type short-float) (y-position 0.0 :type short-float) (x-velocity 0.0 :type short-float) (y-velocity 0.0 :type short-float) (mass *default-ship-mass* :type short-float :read-only t)) \endcode This specifies that each slot always contains a short float, and that the last slot cannot be altered once a ship is constructed.
The available slot-options are: \beginlist \itemitem{\tt \kwd{type} \param{type}} This specifies that the contents of the slot is always of type \param{type}. This is entirely analogous to the declaration of a variable or function; it effectively declares the result type of the reader function. It is implementation-dependent whether the type is checked when initializing a slot or when assigning to it. \param{Type} is not evaluated; it must be a valid type specifier.
\itemitem{\tt \kwd{read-only} \param{x}} When \param{x} is true, this specifies that this slot cannot be altered; it will always contain the value supplied at construction time. \macref{setf} will not accept the reader function for this slot. If \param{x} is false, this slot-option has no effect. \param{X} is not evaluated.
\issue{SETF-FUNCTIONS-AGAIN:MINIMAL-CHANGES} When this option is false or unsupplied, it is implementation-dependent whether the ability to write the slot is implemented by a setf function or a setf expander. \endissue{SETF-FUNCTIONS-AGAIN:MINIMAL-CHANGES}
\endlist
The following keyword options are available for use with \macref{defstruct}. A \macref{defstruct} option can be either a keyword or a list of a keyword and arguments for that keyword; specifying the keyword by itself is equivalent to specifying a list consisting of the keyword and no arguments. The syntax for \macref{defstruct} options differs from the pair syntax used for slot-options. No part of any of these options is evaluated. \beginlist
\itemitem{\kwd{conc-name}}
This provides for automatic prefixing of names of reader (or access) functions. The default behavior is to begin the names of all the reader functions of a structure with the name of the structure followed by a hyphen.
\kwd{conc-name} supplies an alternate prefix to be used. If a hyphen is to be used as a separator, it must be supplied as part of the prefix. If \kwd{conc-name} is \nil\ or no argument is supplied, then no prefix is used; then the names of the reader functions are the same as the slot names. If a non-nil prefix is given, the name of the reader function for each slot is constructed by concatenating that prefix and the name of the slot, and interning the resulting symbol in the package that is current at the time the \macref{defstruct} form is expanded.
Note that no matter what is supplied for \kwd{conc-name}, slot keywords that match the slot names with no prefix attached are used with a constructor function. The reader function name is used in conjunction with \macref{setf}. Here is an example:
\code (defstruct (door (:conc-name dr-)) knob-color width material) \EV DOOR (setq my-door (make-door :knob-color 'red :width 5.0)) \EV #S(DOOR :KNOB-COLOR RED :WIDTH 5.0 :MATERIAL NIL) (dr-width my-door) \EV 5.0 (setf (dr-width my-door) 43.7) \EV 43.7 (dr-width my-door) \EV 43.7 \endcode
Whether or not the \kwd{conc-name} option is explicitly supplied, the following rule governs name conflicts of generated reader (or accessor) names: For any structure type $S\sub 1$ having a reader function named $R$ for a slot named $X\sub 1$ that is inherited by another structure type $S\sub 2$ that would have a reader function with the same name $R$ for a slot named $X\sub 2$, no definition for $R$ is generated by the definition of $S\sub 2$; instead, the definition of $R$ is inherited from the definition of $S\sub 1$. (In such a case, if $X\sub 1$ and $X\sub 2$ are different slots, the implementation might signal a style warning.)
\itemitem{\kwd{constructor}}
This option takes zero, one, or two arguments. If at least one argument is supplied and the first argument is not \nil, then that argument is a symbol which specifies the name of the constructor function. If the argument is not supplied (or if the option itself is not supplied), the name of the constructor is produced by concatenating the string \f{"MAKE-"} and the name of the structure, interning the name in whatever package is current at the time \macref{defstruct} is expanded. If the argument is provided and is \nil, no constructor function is defined.
If \kwd{constructor} is given as \f{(:constructor \param{name} \param{arglist})}, then instead of making a keyword driven constructor function, \macref{defstruct} defines a ``positional'' constructor function, taking arguments whose meaning is determined by the argument's position and possibly by keywords. \param{Arglist} is used to describe what the arguments to the constructor will be. In the simplest case something like \f{(:constructor make-foo (a b c))} defines \f{make-foo} to be a three-argument constructor function whose arguments are used to initialize the slots named \f{a}, \f{b}, and \f{c}.
Because a constructor of this type operates ``By Order of Arguments,'' it is sometimes known as a ``boa constructor.''
For information on how the \param{arglist} for a ``boa constructor'' is processed, \seesection\BoaLambdaLists.
It is permissible to use the \kwd{constructor} option more than once, so that you can define several different constructor functions, each taking different parameters.
\reviewer{Barmar: What about (:constructor) and (:constructor nil). Should we worry about it?}%!!!
\issue{DEFSTRUCT-CONSTRUCTOR-OPTIONS:EXPLICIT} \macref{defstruct} creates the default-named keyword constructor function only if no explicit \kwd{constructor} options are specified, or if the \kwd{constructor} option is specified without a \param{name} argument.
\f{(:constructor nil)} is meaningful only when there are no other \kwd{constructor} options specified. It prevents \macref{defstruct} from generating any constructors at all.
Otherwise, \macref{defstruct} creates a constructor function corresponding to each supplied \kwd{constructor} option. It is permissible to specify multiple keyword constructor functions as well as multiple ``boa constructors''. \endissue{DEFSTRUCT-CONSTRUCTOR-OPTIONS:EXPLICIT}
\itemitem{\kwd{copier}}
This option takes one argument, a symbol, which specifies the name of the copier function. If the argument is not provided or if the option itself is not provided, the name of the copier is produced by concatenating the string \f{"COPY-"} and the name of the structure, interning the name in whatever package is current at the time \macref{defstruct} is expanded. If the argument is provided and is \nil, no copier function is defined.
\issue{DEFSTRUCT-COPIER:ARGUMENT-TYPE} The automatically defined copier function is a function of one argument, \issue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT} which must be of the structure type being defined. \endissue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT} The copier function creates a fresh structure that has the same type as its argument, and that has the same component values as the original structure; that is, the component values are not copied recursively. \issue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT} If the \macref{defstruct} \kwd{type} option was not used, the following equivalence applies:
\code (\param{copier-name} x) = (copy-structure (the \param{structure-name} x)) \endcode \endissue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT} \endissue{DEFSTRUCT-COPIER:ARGUMENT-TYPE}
\itemitem{\kwd{include}}
This option is used for building a new structure definition as an extension of another structure definition. For example:
\code (defstruct person name age sex) \endcode To make a new structure to represent an astronaut that has the attributes of name, age, and sex, and functions that operate on \f{person} structures, \f{astronaut} is defined with \kwd{include} as follows:
\code (defstruct (astronaut (:include person) (:conc-name astro-)) helmet-size (favorite-beverage 'tang)) \endcode
\kwd{include} causes the structure being defined to have the same slots as the included structure. This is done in such a way that the reader functions for the included structure also work on the structure being defined. In this example, an \f{astronaut} therefore has five slots: the three defined in \f{person} and the two defined in \f{astronaut} itself. The reader functions defined by the \f{person} structure can be applied to instances of the \f{astronaut} structure, and they work correctly. Moreover, \f{astronaut} has its own reader functions for components defined by the \f{person} structure. The following examples illustrate the use of \f{astronaut} structures:
\code (setq x (make-astronaut :name 'buzz :age 45. :sex t :helmet-size 17.5)) (person-name x) \EV BUZZ (astro-name x) \EV BUZZ (astro-favorite-beverage x) \EV TANG \endcode \issue{REDUCE-ARGUMENT-EXTRACTION} \code (reduce #'+ astros :key #'person-age) ; obtains the total of the ages ; of the possibly empty ; sequence of astros \endcode \endissue{REDUCE-ARGUMENT-EXTRACTION} The difference between the reader functions \f{person-name} and \f{astro-name} is that \f{person-name} can be correctly applied to any \f{person}, including an \f{astronaut}, while \f{astro-name} can be correctly applied only to an \f{astronaut}. An implementation might check for incorrect use of reader functions.
At most one \kwd{include} can be supplied in a single \macref{defstruct}. The argument to \kwd{include} is required and must be the name of some previously defined structure. If the structure being defined has no \kwd{type} option, then the included structure must also have had no \kwd{type} option supplied for it. If the structure being defined has a \kwd{type} option, then the included structure must have been declared with a \kwd{type} option specifying the same representation type.
If no \kwd{type} option is involved, then the structure name of the including structure definition becomes the name of a data type, and therefore a valid type specifier recognizable by typep; it becomes a subtype of the included structure. In the above example, \f{astronaut} is a subtype of \f{person}; hence
\code (typep (make-astronaut) 'person) \EV true \endcode indicating that all operations on persons also work on astronauts.
The structure using \kwd{include} can specify default values or slot-options for the included slots different from those the included structure specifies, by giving the \kwd{include} option as:
\code (:include \param{included-structure-name} \starparam{slot-description}) \endcode Each \param{slot-description} must have a \param{slot-name} that is the same as that of some slot in the included structure. If a \param{slot-description} has no \param{slot-initform}, then in the new structure the slot has no initial value. Otherwise its initial value form is replaced by the \param{slot-initform} in the \param{slot-description}. A normally writable slot can be made read-only. If a slot is read-only in the included structure, then it must also be so in the including structure. If a type is supplied for a slot, it must be a subtype of the type specified in the included structure.
For example, if the default age for an astronaut is \f{45}, then
\code (defstruct (astronaut (:include person (age 45))) helmet-size (favorite-beverage 'tang)) \endcode
If \kwd{include} is used with the \kwd{type} option, then the effect is first to skip over as many representation elements as needed to represent the included structure, then to skip over any additional elements supplied by the \kwd{initial-offset} option, and then to begin allocation of elements from that point. For example:
\code (defstruct (binop (:type list) :named (:initial-offset 2)) (operator '? :type symbol) operand-1 operand-2) \EV BINOP (defstruct (annotated-binop (:type list) (:initial-offset 3) (:include binop)) commutative associative identity) \EV ANNOTATED-BINOP (make-annotated-binop :operator '* :operand-1 'x :operand-2 5 :commutative t :associative t :identity 1) \EV (NIL NIL BINOP * X 5 NIL NIL NIL T T 1) \endcode The first two \nil\ elements stem from the \kwd{initial-offset} of \f{2} in the definition of \f{binop}. The next four elements contain the structure name and three slots for \f{binop}. The next three \nil\ elements stem from the \kwd{initial-offset} of \f{3} in the definition of \f{annotated-binop}. The last three list elements contain the additional slots for an \f{annotated-binop}.
\itemitem{\kwd{initial-offset}}
\kwd{initial-offset} instructs \macref{defstruct} to skip over a certain number of slots before it starts allocating the slots described in the body. This option's argument is the number of slots \macref{defstruct} should skip. \kwd{initial-offset} can be used only if \kwd{type} is also supplied.
\reviewer{Barmar: What are initial values of the skipped slots?}%!!!
\kwd{initial-offset} allows slots to be allocated beginning at a representational element other than the first. For example, the form
\code (defstruct (binop (:type list) (:initial-offset 2)) (operator '? :type symbol) operand-1 operand-2) \EV BINOP \endcode would result in the following behavior for \f{make-binop}:
\code (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (NIL NIL + X 5) (make-binop :operand-2 4 :operator '*) \EV (NIL NIL * NIL 4) \endcode The selector functions \f{binop-operator}, \f{binop-operand-1}, and {\tt binop-operand-2} would be essentially equivalent to third, fourth, and fifth, respectively. Similarly, the form
\code (defstruct (binop (:type list) :named (:initial-offset 2)) (operator '? :type symbol) operand-1 operand-2) \EV BINOP \endcode would result in the following behavior for \f{make-binop}: \code (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (NIL NIL BINOP + X 5) (make-binop :operand-2 4 :operator '*) \EV (NIL NIL BINOP * NIL 4) \endcode
The first two \nil\ elements stem from the \kwd{initial-offset} of \f{2} in the definition of \f{binop}. The next four elements contain the structure name and three slots for \f{binop}.
\itemitem{\kwd{named}}
\kwd{named} specifies that the structure is named. If no \kwd{type} is supplied, then the structure is always named.
For example:
\code (defstruct (binop (:type list)) (operator '? :type symbol) operand-1 operand-2) \EV BINOP \endcode This defines a constructor function \f{make-binop} and three selector functions, namely \f{binop-operator}, \f{binop-operand-1}, and \f{binop-operand-2}. (It does not, however, define a predicate \f{binop-p}, for reasons explained below.)
The effect of \f{make-binop} is simply to construct a list of length three:
\code (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (+ X 5) (make-binop :operand-2 4 :operator '*) \EV (* NIL 4) \endcode It is just like the function \f{list} except that it takes keyword arguments and performs slot defaulting appropriate to the \f{binop} conceptual data type. Similarly, the selector functions \f{binop-operator}, \f{binop-operand-1}, and \f{binop-operand-2} are essentially equivalent to car, cadr, and caddr, respectively. They might not be completely equivalent because, for example, an implementation would be justified in adding error-checking code to ensure that the argument to each selector function is a length-3 list.
\f{binop} is a conceptual data type in that it is not made a part of the \clisp\ type system. typep does not recognize \f{binop} as a type specifier, and type-of returns \f{list} when given a \f{binop} structure. There is no way to distinguish a data structure constructed by \f{make-binop} from any other list that happens to have the correct structure.
There is not any way to recover the structure name \f{binop} from a structure created by \f{make-binop}. This can only be done if the structure is named. A named structure has the property that, given an instance of the structure, the structure name (that names the type) can be reliably recovered. For structures defined with no \kwd{type} option, the structure name actually becomes part of the \clisp\ data-type system. type-of, when applied to such a structure, returns the structure name as the type of the object; typep recognizes the structure name as a valid type specifier.
For structures defined with a \kwd{type} option, type-of returns a type specifier such as \f{list} or \f{(vector t)}, depending on the type supplied to the \kwd{type} option. The structure name does not become a valid type specifier. However, if the \kwd{named} option is also supplied, then the first component of the structure (as created by a \macref{defstruct} constructor function) always contains the structure name. This allows the structure name to be recovered from an instance of the structure and allows a reasonable predicate for the conceptual type to be defined: the automatically defined \param{name-p} predicate for the structure operates by first checking that its argument is of the proper type (\typeref{list}, \f{(vector t)}, or whatever) and then checking whether the first component contains the appropriate type name.
Consider the \f{binop} example shown above, modified only to include the \kwd{named} option:
\code (defstruct (binop (:type list) :named) (operator '? :type symbol) operand-1 operand-2) \EV BINOP \endcode As before, this defines a constructor function \f{make-binop} and three selector functions \f{binop-operator}, \f{binop-operand-1}, and \f{binop-operand-2}. It also defines a predicate \f{binop-p}. The effect of \f{make-binop} is now to construct a list of length four:
\code (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (BINOP + X 5) (make-binop :operand-2 4 :operator '*) \EV (BINOP * NIL 4) \endcode The structure has the same layout as before except that the structure name \f{binop} is included as the first list element. The selector functions \f{binop-operator}, \f{binop-operand-1}, and \f{binop-operand-2} are essentially equivalent to cadr, caddr, and cadddr, respectively. The predicate \f{binop-p} is more or less equivalent to this definition:
\code (defun binop-p (x) (and (consp x) (eq (car x) 'binop))) \EV BINOP-P \endcode The name \f{binop} is still not a valid type specifier recognizable to typep, but at least there is a way of distinguishing \f{binop} structures from other similarly defined structures.
\itemitem{\kwd{predicate}}
This option takes one argument, which specifies the name of the type predicate. If the argument is not supplied or if the option itself is not supplied, the name of the predicate is made by concatenating the name of the structure to the string \f{"-P"}, interning the name in whatever package is current at the time \macref{defstruct} is expanded. If the argument is provided and is \nil, no predicate is defined. A predicate can be defined only if the structure is named; if \kwd{type} is supplied and \kwd{named} is not supplied, then \kwd{predicate} must either be unsupplied or have the value \nil.
\issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \itemitem{\kwd{print-function}, \kwd{print-object}}
The \kwd{print-function} and \kwd{print-object} options specify that a print-object method for structures of type \param{structure-name} should be generated. These options are not synonyms, but do perform a similar service; the choice of which option (\kwd{print-function} or \kwd{print-object}) is used affects how the function named \param{printer-name} is called. Only one of these options may be used, and these options may be used only if \kwd{type} is not supplied.
If the \kwd{print-function} option is used, then when a structure of type \param{structure-name} is to be printed, the designated printer function is called on three arguments:
\beginlist
\itemitem{--} the structure to be printed (a generalized instance of \param{structure-name}).
\itemitem{--} a stream to print to.
\itemitem{--} an integer indicating the current depth. The magnitude of this integer may vary between implementations; however, it can reliably be compared against \varref{*print-level*} to determine whether depth abbreviation is appropriate.
\endlist
Specifying \f{(:print-function \param{printer-name})} is approximately equivalent to specifying:
\code (defmethod print-object ((object \param{structure-name}) stream) (funcall (function \param{printer-name}) object stream \metaparam{current-print-depth})) \endcode
where the \metaparam{current-print-depth} represents the printer's belief of how deep it is currently printing. It is implementation-dependent whether \metaparam{current-print-depth} is always 0 and \param{*print-level*}, if non-nil, is re-bound to successively smaller values as printing descends recursively, or whether \param{current-print-depth} varies in value as printing descends recursively and \param{*print-level*} remains constant during the same traversal.
If the \kwd{print-object} option is used, then when a structure of type \param{structure-name} is to be printed, the designated printer function is called on two arguments:
\beginlist
\itemitem{--} the structure to be printed.
\itemitem{--} the stream to print to.
\endlist
Specifying \f{(:print-object \param{printer-name})} is equivalent to specifying:
\code (defmethod print-object ((object \param{structure-name}) stream) (funcall (function \param{printer-name}) object stream)) \endcode
\issue{DEFSTRUCT-PRINT-FUNCTION-INHERITANCE:YES} \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} If no \kwd{type} option is supplied, and if either a \kwd{print-function} or a \kwd{print-object} option is supplied, and if no \param{printer-name} is supplied, then a print-object method specialized for \param{structure-name} is generated that calls a function that implements the default printing behavior for structures using \f{\#S} notation; \seesection\PrintingStructures.
If neither a \kwd{print-function} nor a \kwd{print-object} option is supplied, then \macref{defstruct} does not generate a print-object method specialized for \param{structure-name} and some default behavior is inherited either from a structure named in an \kwd{include} option or from the default behavior for printing structures; \seefun{print-object} and \secref\PrintingStructures. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93} \endissue{DEFSTRUCT-PRINT-FUNCTION-INHERITANCE:YES}
\issue{PRINT-CIRCLE-STRUCTURE:USER-FUNCTIONS-WORK} When \varref{*print-circle*} is true, a user-defined print function can print objects to the supplied stream using write, prin1, princ, or format and expect circularities to be detected and printed using the \f{\#\i{n}\#} syntax. This applies to methods on print-object in addition to \kwd{print-function} options. If a user-defined print function prints to a stream other than the one that was supplied, then circularity detection starts over for that stream. \Seevar{*print-circle*}. \endissue{PRINT-CIRCLE-STRUCTURE:USER-FUNCTIONS-WORK}
\endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
\itemitem{\kwd{type}}
\kwd{type} explicitly specifies the representation to be used for the structure. Its argument must be one of these types:
\beginlist \itemitem{\typeref{vector}}
This produces the same result as specifying {\tt (vector t)}. The structure is represented as a general vector, storing components as vector elements. The first component is vector element 1 if the structure is \kwd{named}, and element 0 otherwise.
\reviewer{Barmar: Do any implementations create non-simple vectors?}%!!!
\itemitem{\f{(vector \param{element-type})}}
The structure is represented as a (possibly specialized) vector, storing components as vector elements. Every component must be of a type that can be stored in a vector of the type specified. The first component is vector element 1 if the structure is \kwd{named}, and element 0 otherwise. The structure can be \kwd{named} only if \thetype{symbol} is a subtype of the supplied \param{element-type}.
\itemitem{\typeref{list}}
The structure is represented as a list. The first component is the cadr if the structure is \kwd{named}, and the car if it is not \kwd{named}. \endlist
Specifying this option has the effect of forcing a specific representation and of forcing the components to be stored in the order specified in \macref{defstruct} in corresponding successive elements of the specified representation. It also prevents the structure name from becoming a valid type specifier recognizable by typep.
For example:
\code (defstruct (quux (:type list) :named) x y) \endcode
should make a constructor that builds a list exactly like the one that list produces, with {\tt quux} as its car.
If this type is defined:
\code (deftype quux () '(satisfies quux-p)) \endcode then this form
\code (typep (make-quux) 'quux) \endcode should return precisely what this one does
\code (typep (list 'quux nil nil) 'quux) \endcode
If \kwd{type} is not supplied, the structure is represented as an object \oftype{structure-object}.
\macref{defstruct} without a \kwd{type} option defines a class with the structure name as its name. The metaclass of structure instances is \typeref{structure-class}.
\endlist
\issue{DEFSTRUCT-REDEFINITION:ERROR} The consequences of redefining a \macref{defstruct} structure are undefined. \endissue{DEFSTRUCT-REDEFINITION:ERROR}
In the case where no \macref{defstruct} options have been supplied, the following functions are automatically defined to operate on instances of the new structure:
\beginlist \itemitem{\bf Predicate}
A predicate with the name \f{\param{structure-name}-p} is defined to test membership in the structure type. The predicate \f{(\param{structure-name}-p \param{object})} is true if an \param{object} is of this type; otherwise it is false. typep can also be used with the name of the new type to test whether an object belongs to the type. Such a function call has the form \hbox{\f{(typep \param{object} '\param{structure-name})}}.
\itemitem{\bf Component reader functions}
Reader functions are defined to read the components of the structure. For each slot name, there is a corresponding reader function with the name \f{\param{structure-name}-\param{slot-name}}. This function reads the contents of that slot. Each reader function takes one argument, which is an instance of the structure type. \macref{setf} can be used with any of these reader functions to alter the slot contents.
\itemitem{\bf Constructor function}
A constructor function with the name \f{make-\param{structure-name}} is defined. This function creates and returns new instances of the structure type.
\itemitem{\bf Copier function}
A copier function with the name \f{copy-\param{structure-name}} is defined. The copier function takes an object of the structure type and creates a new object of the same type that is a copy of the first. The copier function creates a new structure with the same component entries as the original. Corresponding components of the two structure instances are eql. \endlist
\issue{COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY} If a \macref{defstruct} form appears as a top level form, the compiler must make the structure type name recognized as a valid type name in subsequent declarations (as for \macref{deftype}) and make the structure slot readers known to \macref{setf}. In addition, the compiler must save enough information about the structure type so that further \macref{defstruct} definitions can use \kwd{include} in a subsequent \macref{deftype} in the same file to refer to the structure type name. The functions which \macref{defstruct} generates are not defined in the compile time environment, although the compiler may save enough information about the functions to code subsequent calls inline. The \f{\#S} reader macro might or might not recognize the newly defined structure type name at compile time. \endissue{COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY}
\label Examples:: An example of a structure definition follows:
\code (defstruct ship x-position y-position x-velocity y-velocity mass) \endcode This declares that every \f{ship} is an object with five named components. The evaluation of this form does the following:
\beginlist \itemitem{1.} It defines \f{ship-x-position} to be a function of one argument, a ship, that returns the \f{x-position} of the ship; \f{ship-y-position} and the other components are given similar function definitions. These functions are called the access functions, as they are used to access elements of the structure.
\itemitem{2.} \f{ship} becomes the name of a type of which instances of ships are elements. \f{ship} becomes acceptable to typep, for example; \f{(typep x 'ship)} is true if \f{x} is a ship and false if \f{x} is any object other than a ship.
\itemitem{3.} A function named \f{ship-p} of one argument is defined; it is a predicate that is true if its argument is a ship and is false otherwise.
\itemitem{4.} A function called \f{make-ship} is defined that, when invoked, creates a data structure with five components, suitable for use with the access functions. Thus executing
\code (setq ship2 (make-ship)) \endcode sets \f{ship2} to a newly created \f{ship} object. One can supply the initial values of any desired component in the call to \f{make-ship} by using keyword arguments in this way:
\code (setq ship2 (make-ship :mass *default-ship-mass* :x-position 0 :y-position 0)) \endcode This constructs a new ship and initializes three of its components. This function is called the ``constructor function'' because it constructs a new structure.
\itemitem{5.} A function called \f{copy-ship} of one argument is defined that, when given a \f{ship} object, creates a new \f{ship} object that is a copy of the given one. This function is called the ``copier function.'' \endlist
\macref{setf} can be used to alter the components of a \f{ship}:
\code (setf (ship-x-position ship2) 100) \endcode This alters the \f{x-position} of \f{ship2} to be \f{100}. This works because \macref{defstruct} behaves as if it generates an appropriate \macref{defsetf} for each access function.
\code ;;; ;;; Example 1 ;;; define town structure type ;;; area, watertowers, firetrucks, population, elevation are its components ;;; (defstruct town area watertowers (firetrucks 1 :type fixnum) ;an initialized slot population (elevation 5128 :read-only t)) ;a slot that can't be changed \EV TOWN ;create a town instance (setq town1 (make-town :area 0 :watertowers 0)) \EV #S(TOWN...) ;town's predicate recognizes the new instance (town-p town1) \EV true ;new town's area is as specified by make-town (town-area town1) \EV 0 ;new town's elevation has initial value (town-elevation town1) \EV 5128 ;setf recognizes reader function (setf (town-population town1) 99) \EV 99 (town-population town1) \EV 99 ;copier function makes a copy of town1 (setq town2 (copy-town town1)) \EV #S(TOWN...) (= (town-population town1) (town-population town2)) \EV true ;since elevation is a read-only slot, its value can be set only ;when the structure is created (setq town3 (make-town :area 0 :watertowers 3 :elevation 1200)) \EV #S(TOWN...) ;;; ;;; Example 2 ;;; define clown structure type ;;; this structure uses a nonstandard prefix ;;; (defstruct (clown (:conc-name bozo-)) (nose-color 'red) frizzy-hair-p polkadots) \EV CLOWN (setq funny-clown (make-clown)) \EV #S(CLOWN) ;use non-default reader name (bozo-nose-color funny-clown) \EV RED (defstruct (klown (:constructor make-up-klown) ;similar def using other (:copier clone-klown) ;customizing keywords (:predicate is-a-bozo-p)) nose-color frizzy-hair-p polkadots) \EV klown ;custom constructor now exists (fboundp 'make-up-klown) \EV true ;;; ;;; Example 3 ;;; define a vehicle structure type ;;; then define a truck structure type that includes ;;; the vehicle structure ;;; (defstruct vehicle name year (diesel t :read-only t)) \EV VEHICLE (defstruct (truck (:include vehicle (year 79))) load-limit (axles 6)) \EV TRUCK (setq x (make-truck :name 'mac :diesel t :load-limit 17)) \EV #S(TRUCK...) ;vehicle readers work on trucks (vehicle-name x) \EV MAC ;default taken from :include clause (vehicle-year x) \EV 79 (defstruct (pickup (:include truck)) ;pickup type includes truck camper long-bed four-wheel-drive) \EV PICKUP (setq x (make-pickup :name 'king :long-bed t)) \EV #S(PICKUP...) ;:include default inherited (pickup-year x) \EV 79 ;;; ;;; Example 4 ;;; use of BOA constructors ;;; (defstruct (dfs-boa ;BOA constructors (:constructor make-dfs-boa (a b c)) (:constructor create-dfs-boa (a &optional b (c 'cc) &rest d &aux e (f 'ff)))) a b c d e f) \EV DFS-BOA ;a, b, and c set by position, and the rest are uninitialized (setq x (make-dfs-boa 1 2 3)) \EV #(DFS-BOA...) (dfs-boa-a x) \EV 1 ;a and b set, c and f defaulted (setq x (create-dfs-boa 1 2)) \EV #(DFS-BOA...) (dfs-boa-b x) \EV 2 (eq (dfs-boa-c x) 'cc) \EV true ;a, b, and c set, and the rest are collected into d (setq x (create-dfs-boa 1 2 3 4 5 6)) \EV #(DFS-BOA...) (dfs-boa-d x) \EV (4 5 6) \endcode
\label Affected By:\None.
\label Exceptional Situations::
\issue{DEFSTRUCT-SLOTS-CONSTRAINTS-NAME:DUPLICATES-ERROR} If any two slot names (whether present directly or inherited by the \kwd{include} option) are the same under string=, \macref{defstruct} should signal an error \oftype{program-error}. \endissue{DEFSTRUCT-SLOTS-CONSTRAINTS-NAME:DUPLICATES-ERROR}
\issue{DATA-TYPES-HIERARCHY-UNDERSPECIFIED} The consequences are undefined if the \param{included-structure-name} does not name a structure type. \endissue{DATA-TYPES-HIERARCHY-UNDERSPECIFIED}
\label See Also::
documentation, print-object, \macref{setf}, subtypep, type-of, typep, {\secref\Compilation}
\label Notes::
The \param{printer-name} should observe the values of such printer-control variables as \varref{*print-escape*}.
The restriction against issuing a warning for type mismatches between a \param{slot-initform} and the corresponding slot's \kwd{type} option is necessary because a \param{slot-initform} must be specified in order to specify slot options; in some cases, no suitable default may exist.
The mechanism by which \macref{defstruct} arranges for slot accessors to be usable with \macref{setf} is implementation-dependent; for example, it may use setf functions, setf expanders, or some other implementation-dependent mechanism known to that implementation's code for \macref{setf}.