Common Lisp the Language, 2nd Edition
Next: Defstruct Options
Up: Structures
Previous: Using the
Automatically
Each slot-description in a defstruct
form may
specify one or more slot-options. A 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:
(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))
This specifies that each slot will always contain a short-format floating-point number, and that the last slot may not be altered once a ship is constructed.
The available slot-options are as follows.
:type
The option :type``
type
specifies that
the contents of the slot will always be of the specified data type. This
is entirely analogous to the declaration of a variable or function;
indeed, it effectively declares the result type of the access function.
An implementation may or may not choose to check the type of the new
object when initializing or assigning to a slot. Note that the argument
form type is not evaluated; it must be a valid type
specifier.
:read-only
The option :read-only``
x
, where
x is not nil
, specifies that this slot may not be
altered; it will always contain the value specified at construction
time. setf
will not accept the access function for this
slot. If x is nil
, this slot-option has no effect.
Note that the argument form x is not evaluated.
Note that it is impossible to specify a slot-option unless a default value is specified first.
Next: Defstruct Options
Up: Structures
Previous: Using the
Automatically
AI.Repository@cs.cmu.edu