#reader(lib "docreader.ss" "scribble") @require["mz.ss"] @title[#:tag "mz:structures"]{Structures} A @pidefterm{structure type} is a record datatype composing a number of @idefterm{fields}. A @pidefterm{structure}, an instance of a structure type, is a first-class value that contains a value for each field of the structure type. A structure instance is created with a type-specific @tech{constructor} procedure, and its field values are accessed and changed with type-specific @tech{accessor} and @tech{mutator} procedures. In addition, each structure type has a @tech{predicate} procedure that answers @scheme[#t] for instances of the structure type and @scheme[#f] for any other value. A structure type's fields are essentially unnamed, though names are supported for error-reporting purposes. The constructor procedure takes one value for each field of the structure type, except that some of the fields of a structure type can be @deftech{automatic fields}; the @tech{automatic fields} are initialized to a constant that is associated with the structure type, and the corresponding arguments are omitted for the constructor procedure. All automatic fields in a structure type follow the non-automatic fields. A structure type can be created as a @pidefterm{structure subtype} of an existing base structure type. An instance of a structure subtype can always be used as an instance of the base structure type, but the subtype gets its own predicate procedure, and it may have its own fields in addition to the fields of the base type. A structure subtype ``inherits'' the fields of its base type. If the base type has @math{m} fields, and if @math{n} fields are specified for the new structure subtype, then the resulting structure type has @math{m+n} fields. The value for automatic fields can be different in a subtype than in its base type. If @math{m'} of the original @math{m} fields are non-automatic (where @math{m'symbol (format "make-~a" name)) "second field must be a number")) (values a1 (exact->inexact a2) b1)))) (make-c 'x 'y 'z) (define a-c (make-c 'x 2 'z)) (a-ref a-c 1) ]} @defproc[(make-struct-field-accessor [accessor-proc procedure?] [field-pos-k exact-nonnegative-integer?] [field-name symbol?]) procedure?]{ Returns a field accessor that is equivalent to @scheme[(lambda (s) (accessor-proc s field-pos-k))]. The @scheme[accessor-proc] must be an @tech{accessor} returned by @scheme[make-struct-type]. The name of the resulting procedure for debugging purposes is derived from @scheme[field-name] and the name of @scheme[accessor-proc]'s structure type. For examples, see @scheme[make-struct-type].} @defproc[(make-struct-field-mutator [mutator-proc procedure?] [field-pos-k exact-nonnegative-integer?] [field-name symbol?]) procedure?]{ Returns a field mutator that is equivalent to @scheme[(lambda (s v) (mutator-proc s field-pos-k v))]. The @scheme[mutator-proc] must be a @tech{mutator} returned by @scheme[make-struct-type]. The name of the resulting procedure for debugging purposes is derived from @scheme[field-name] and the name of @scheme[mutator-proc]'s structure type. For examples, see @scheme[make-struct-type].}