4.1 Defining Structure Types: struct
Programmer-Defined Datatypes in Guide: Racket introduces struct.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
A struct form with n fields defines up to 4+2n names:
struct:id, a structure type descriptor value that represents the structure type.
constructor-id (which defaults to id), a constructor procedure that takes m arguments and returns a new instance of the structure type, where m is the number of fields that do not include an #:auto option.
id?, a predicate procedure that returns #t for instances of the structure type (constructed by constructor-id or the constructor for a subtype) and #f for any other value.
id-field-id, for each field; an accessor procedure that takes an instance of the structure type and extracts the value for the corresponding field.
set-id-field-id!, for each field that includes a #:mutable option, or when the #:mutable option is specified as a struct-option; a mutator procedure that takes an instance of the structure type and a new field value. The structure is destructively updated with the new value, and #<void> is returned.
id, a transformer binding that encapsulates information about the structure type declaration. This binding is used to define subtypes, and it also works with the shared and match forms. For detailed information about the binding of id, see Structure Type Transformer Binding.
The constructor-id and id can be the same, in which case id performs both roles.
If super-id is provided, it must have a transformer binding of the same sort bound to id (see Structure Type Transformer Binding), and it specifies a supertype for the structure type. Alternately, the #:super option can be used to specify an expression that must produce a structure type descriptor. See Structures for more information on structure subtypes and supertypes. If both super-id and #:super are provided, a syntax error is reported.
If the #:mutable option is specified for an individual field, then the field can be mutated in instances of the structure type, and a mutator procedure is bound. Supplying #:mutable as a struct-option is the same as supplying it for all fields. If #:mutable is specified as both a field-option and struct-option, a syntax error is reported.
The #:inspector, #:auto-value, and #:guard options specify an inspector, value for automatic fields, and guard procedure, respectively. See make-struct-type for more information on these attributes of a structure type. The #:property option, which is the only one that can be supplied multiple times, attaches a property value to the structure type; see Structure Type Properties for more information on properties. The #:transparent option is a shorthand for #:inspector #f.
Use the prop:procedure to property implement an applicable structure, use prop:evt to create a structure type whose instances are synchronizable events, and so on. By convention, property names start with prop:.
The #:prefab option obtains a prefab (pre-defined, globally shared) structure type, as opposed to creating a new structure type. Such a structure type is inherently transparent and cannot have a guard or properties, so using #:prefab with #:transparent, #:inspector, #:guard, or #:property is a syntax error. If a supertype is specified, it must also be a prefab structure type.
If constructor-id is supplied, then the transformer binding of id records constructor-id as the constructor binding; as a result, for example, struct-out includes constructor-id as an export. If constructor-id is supplied via #:extra-constructor-name and it is not id, applying object-name on the constructor produces the symbolic form of id rather than constructor-id. If constructor-id is supplied via #:constructor-name and it is not the same as id, then id does not serve as a constructor, and object-name on the constructor produces the symbolic form of constructor-id.
If the #:omit-define-syntaxes option is supplied, then id is not bound as a transformer. If the #:omit-define-values option is supplied, then none of the usual variables are bound, but id is bound. If both are supplied, then the struct form is equivalent to (begin).
If #:auto is supplied as a field-option, then the constructor procedure for the structure type does not accept an argument corresponding to the field. Instead, the structure type’s automatic value is used for the field, as specified by the #:auto-value option, or as defaults to #f when #:auto-value is not supplied. The field is mutable (e.g., through reflective operations), but a mutator procedure is bound only if #:mutable is specified.
If a field includes the #:auto option, then all fields after it must also include #:auto, otherwise a syntax error is reported. If any field-option or struct-option keyword is repeated, other than #:property, a syntax error is reported.
For serialization, see define-serializable-struct.
Examples: | |||
| |||
> (posn 1 2) | |||
(posn 1 2 0) | |||
> (posn? (posn 1 2)) | |||
#t | |||
> (posn-y (posn 1 2)) | |||
2 |
| ||||||
| ||||||
|
(struct-field-index field-id) |
Examples: | ||
| ||
| ||
> (happy+ 2) | ||
3 | ||
> (mood-procedure-rating happy+) | ||
10 |
| ||||||||||
|
This form is provided for backward compatibility; struct is preferred.
Examples: | ||||
| ||||
> (make-posn 1 2) | ||||
(posn 1 2 0) | ||||
> (posn? (make-posn 1 2)) | ||||
#t | ||||
> (posn-y (make-posn 1 2)) | ||||
2 |
|
Examples: | |||||
| |||||
> (define-xy-struct posn) | |||||
> (posn-x (make-posn 1 2)) | |||||
1 | |||||
> (define-xy-struct posn #:mutable) | |||||
> (set-posn-x! (make-posn 1 2) 0) | |||||
> (define-xy-struct posn #:bad-option) | |||||
eval:24:0: define-xy-struct: unrecognized | |||||
struct-specification keyword at: #:bad-option in: | |||||
(define-xy-struct posn #:bad-option) |