#
Chapter 41
`struct.ss`: Structure Utilities

To load: `(require (lib "struct.ss"))`

`(`copy-struct` `struct-id struct-expr (accessor-id field-expr) `···``)` S Y N T A X

This form provides ``functional update'' for structure instances. The result of evaluating `struct-expr` must be an instance of the structure type named by `struct-id`. The result of the `copy-struct` expression is a fresh instance of `struct-id` with the same field values as the result of `struct-expr`, except that the value for the field accessed by each `accessor-id` is replaced by the result of `field-expr`.

The result of `struct-expr` might be an instance of a sub-type of `struct-id`, but the result of the `copy-struct` expression is an immediate instance of `struct-id`. If `struct-expr` does not produce an instance of `struct-id`, the `exn:fail:contract` exception is raised.

If any `accessor-id` is not bound to an accessor of `struct-id` (according to the expansion-time information associated with `struct-id`), or if the same `accessor-id` is used twice, then a syntax error is raised.

`(`define-struct/properties` `id (field-id `···`) ((prop-expr val-expr) `···`) [inspector-expr]`)` S Y N T A X

Like `define-struct`, but properties (see section 4.3 in PLT MzScheme: Language Manual) can be attached to the structure type. Each `prop-expr` should produce a structure-type property value, and each `val-expr` produces the corresponding value for the property.

Example:

    (define-struct/properties point (x y)
       ([prop:custom-write (lambda (p port write?)
                            (fprintf port "(~a, ~a)"
                                          (point-x p)
                                          (point-y p)))]))

    (display (make-point 1 2)) ; prints (1, 2)

`(`make-->vector` `struct-id`)` S Y N T A X

This form builds a function that accepts a struct instance (matching `struct-id`) and provides a vector of the fields of the struct.