6.6 mischief/struct: User-Defined Datatypes
(require mischief/struct) | package: mischief |
6.6.1 Updating Structure Fields
syntax
(update struct-id val-expr [field-id val-expr] ...)
Constructs a version of val-expr, which must be an instance of
struct-id, where the value of each field named field-id is
replaced by the result of the corresponding val-expr. Each
field-id is bound to the field’s original value when executing any of
the val-exprs.
Examples:
> (struct both [one two] #:transparent)
> (define (swap b) (update both b [one two] [two one])) > (swap (both 1 2)) (both 2 1)
6.6.2 Prefab Structures
procedure
k : prefab-key? v : any/c
Constructs a prefab structure with key k and one field for each
v.
Example:
> (prefab 'posn 1 2) '#s(posn 1 2)
Reports whether x is a prefab structure.
Examples:
procedure
(prefab-of-key? key x) → boolean?
key : prefab-key? x : any/c
Reports whether x is a prefab structure whose key is equivalent to
k.
Examples:
> (prefab-of-key? 'posn (prefab 'posn 1 2)) #t
> (prefab-of-key? 'posn (prefab 'point 1 2)) #f
> (prefab-of-key? 'posn '(posn 1 2)) #f
procedure
(prefab-key x) → prefab-key?
x : prefab?
Produces the key of a prefab struct. The result is always normalized in the
sense of normalize-prefab-key.
Example:
> (prefab-key (prefab 'posn 1 2)) 'posn
procedure
(prefab-fields x) → list?
x : prefab?
Produces a list of the fields of a prefab struct.
Example:
> (prefab-fields (prefab 'posn 1 2)) '(1 2)
procedure
(prefab-ref x i) → any/c
x : prefab? i : exact-nonnegative-integer?
Extracts the ith field of x, indexed from 0.
Examples:
> (prefab-ref (prefab 'posn 1 2) 0) 1
> (prefab-ref (prefab 'posn 1 2) 1) 2
procedure
(prefab-type-name x) → symbol?
x : prefab?
Produces the symbol representing the name of a prefab struct’s key.
Example:
> (prefab-type-name (prefab 'posn 1 2)) 'posn
procedure
(normalize-prefab-key key) → prefab-key?
key : prefab-key?
Normalizes key. For any two prefab struct keys k1 and
k2, the keys represent the same prefab struct type if and only if
(equal? (normalize-prefab-key k1) (normalize-prefab-key k2)).
Examples:
> (normalize-prefab-key 'posn) 'posn
> (normalize-prefab-key '(posn)) 'posn
6.6.3 Transparent Structures
procedure
(transparent-struct? x [ #:inspector inspector]) → boolean? x : any/c inspector : inspector? = (current-inspector)
Reports whether x is a transparent structure, i.e., inspector
can access its structure type and all of its fields.
procedure
(transparent-struct-type x [ #:inspector inspector]) → struct-type? x : transparent-struct? inspector : inspector? = (current-inspector)
Produces the structure type of x.
procedure
(transparent-struct-predicate x [ #:inspector inspector]) → predicate/c x : transparent-struct? inspector : inspector? = (current-inspector)
Produces a predicate that recognizes values belonging to the same structure
type as x.
procedure
(transparent-struct-constructor x [ #:inspector inspector]) → (-> any/c ... transparent-struct?) x : transparent-struct? inspector : inspector? = (current-inspector)
Produces a procedure that constructs transparent structs of the same type as
x.
procedure
(transparent-struct-ref x i [ #:inspector inspector]) → any/c x : transparent-struct? i : exact-nonnegative-integer? inspector : inspector? = (current-inspector)
Produces the value of the ith field of x, indexed from 0.
procedure
(transparent-struct-type-name x [ #:inspector inspector]) → symbol? x : transparent-struct? inspector : inspector? = (current-inspector)
Produces the name of x’s structure type.
procedure
(transparent-struct-fields x [ #:inspector inspector]) → list? x : transparent-struct? inspector : inspector? = (current-inspector)
Produces a list of the values of x’s fields.