5.3 Structure Type Properties (original) (raw)

5.3 Structure Type Properties🔗

Generic Interfaces provide a high-level API on top of structure type properties.

A structure type property allows per-type information to be associated with a structure type (as opposed to per-instance information associated with a structure value). A property value is associated with a structure type through themake-struct-type procedure (seeCreating Structure Types) or through the #:property option of struct. Subtypes inherit the property values of their parent types, and subtypes can override an inherited property value with a new value.

Creates a new structure type property and returns three values:

If the optional guard is supplied as a procedure, it is called by make-struct-type before attaching the property to a new structure type. The guard must accept two arguments: a value for the property supplied to make-struct-type, and a list containing information about the new structure type. The list contains the values that struct-type-info would return for the new structure type if it skipped the current-inspector control checks.

The result of calling guard is associated with the property in the target structure type, instead of the value supplied tomake-struct-type. To reject a property association (e.g., because the value supplied to make-struct-type is inappropriate for the property), the guard can raise an exception. Such an exception prevents make-struct-type from returning a structure type descriptor.

If guard is 'can-impersonate, then the property’s accessor can be redirected throughimpersonate-struct. This option is identical to supplying#t as the can-impersonate? argument and is provided for backwards compatibility.

The optional supers argument is a list of properties that are automatically associated with some structure type when the newly created property is associated to the structure type. Each property insupers is paired with a procedure that receives the value supplied for the new property (after it is processed byguard) and returns a value for the associated property (which is then sent to that property’s guard, of any).

The optional can-impersonate? argument determines if the structure type property can be redirected through impersonate-struct. If the argument is #f, then redirection is not allowed. Otherwise, the property accessor may be redirected by a struct impersonator.

The optional accessor-name argument supplies a name (in the sense of object-name) to use for the returned accessor function. If accessor-name is #f, a name is created by adding -accessor to the end of name.

The optional contract-str argument supplies a contract that is included in an error message with the returned accessor is applied to a value that is not an instance of the property (and where afailure-result argument is not supplied to the accessor). Ifcontract-str is #f, a contract is created by adding? to the end of name.

The optional realm argument supplies a realm (in the sense of procedure-realm) to associate with the returned accessor.

Examples:

> (define-values (prop:p p? p-ref) (make-struct-type-property 'p))
> (p? struct:a)
#t
> (p? 13)
#f
> (define an-a (make-a 'x 'y))
> (p? an-a)
#t
> (p-ref an-a)
8
> (p? struct:b)
#f
> (q-ref struct:c)
9
> (p-ref struct:c)
3

Changed in version 7.0 of package base: The CS implementation of Racket skips the inspector check for exposing an ancestor structure type, if any, in information provided to a guard procedure.
Changed in version 8.4.0.2: Added the accessor-name,contract-str, andrealm arguments.
Changed in version 8.5.0.2: Changed the BC implementation of Racket to skip the inspector check, the same as the CS implementation, for ancestor information provided to a guard procedure.

Returns #t if v is a structure type property descriptor value, #f otherwise.

Returns #t if v is an accessor procedure produced by make-struct-type-property, #f otherwise.

Returns #t if v is a predicate procedure produced bymake-struct-type-property and either prop is#f or it was produced by the same call tomake-struct-type-property, #f otherwise.

Added in version 7.5.0.11 of package base.