Next: , Previous: , Up: syslib structs safe   [Index]


12.14.1.2 Using structure types

Syntax: struct-type-descriptor ?type-name

Evaluate to the type descriptor of the data structure ?type-name, which must be the first argument to a previous use of define-struct.

Function: struct-type-descriptor? obj

Return #t if obj is a struct–type descriptor, otherwise return #f. This predicate returns #f when applied to R6RS record–type descriptors.

Function: struct-type-constructor std

Return a constructor function for struct of type defined by the type descriptor std. The returned constructor accepts as many arguments as there are fields specified by std and it returns a new struct instance.

(define duo-std
  (make-struct-type "duo" '(one two)))

(define make-duo
  (struct-type-constructor duo-std))

(make-duo 1 2)  ⇒ #[struct duo one=1 two=2]
Function: struct-type-predicate std

Return a predicate function for structs of type defined by the type descriptor std.

(define duo-std
  (make-struct-type "duo" '(one two)))

(define make-duo
  (struct-type-constructor duo-std))

(define duo?
  (struct-type-predicate duo-std))

(duo? (make-duo 1 2))   ⇒ #t
Function: struct-type-field-accessor std index/name
Function: struct-type-field-mutator std index/name

Return an accessor or mutator function for the field at index/name of structs of type defined by the type descriptor std. index/name can be a field index or a symbol representing a field name.

(define-struct color
  (red green blue))

(define stru
  (make-color 1 2 3))

((struct-type-field-accessor (struct-type-descriptor color) 'red)
 stru)
⇒ 1

((struct-type-field-accessor (struct-type-descriptor color) 0)
 stru)
⇒ 1
Function: struct-type-field-method std index/name

Return a “method” function for the field at index/name of structs of type defined by the type descriptor std. index/name can be a field index or a symbol representing a field name. When the method function is applied to 1 argument: it behaves like a field accessor. When the method function is applied to 2 arguments: it behaves like a field mutator.

(define-struct color
  (red green blue))

(define stru
  (make-color 1 2 3))

(define red-method
  (struct-type-field-method (struct-type-descriptor color) 'red))

(red-method stru)       ⇒ 1
(red-method stru 11)
(red-method stru)       ⇒ 11

Next: , Previous: , Up: syslib structs safe   [Index]