5 Structures (original) (raw)

5 Structures🔗

+Programmer-Defined Datatypes in The Racket Guide introduces structure types via struct.

A structure type is a record datatype composing a number offields. A 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 constructor procedure, and its field values are accessed and changed with type-specific accessor andmutator procedures. In addition, each structure type has apredicate procedure that answers #t for instances of the structure type and #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 automatic fields; the automatic fields are initialized to a constant that is associated with the structure type, and the corresponding arguments are omitted from the constructor procedure. All automatic fields in a structure type follow the non-automatic fields.

A structure type can be created as a 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 m fields, and if n fields are specified for the new structure subtype, then the resulting structure type hasm+n fields. The value for automatic fields can be different in a subtype than in its base type.

If m′ of the original m fields are non-automatic (wherem′<m), and n′ of the new fields are non-automatic (wheren′<n), then m′+n′ field values must be provided to the subtype’s constructor procedure. Values for the first m fields of a subtype instance are accessed with selector procedures for the original base type (or its supertypes), and the last n are accessed with subtype-specific selectors. Subtype-specificaccessors and mutators for the first m fields do not exist.

The struct form and make-struct-typeprocedure typically create a new structure type, but they can also access prefab (i.e., previously fabricated) structure types that are globally shared, and whose instances can be parsed and written by the default reader (see The Reader) and printer (seeThe Printer). Prefab structure types can inherit only from other prefab structure types, and they cannot have guards (seeCreating Structure Types) or properties (seeStructure Type Properties). Exactly one prefab structure type exists for each combination of name, supertype, field count, automatic field count, automatic field value (when there is at least one automatic field), and field mutability.

+Serialization also provides information on reading and writing structures.

Two structure values areeqv? if and only if they are eq?. Two structure values are equal? if they are eq?. By default, two structure values are also equal? if they are instances of the same structure type, no fields are opaque, and the results of applyingstruct->vector to the structs areequal?. (Consequently, equal? testing for structures may depend on the current inspector.) A structure type can override the default equal? definition through thegen:equal+hash or gen:equal-mode+hash generic interface.