8.16.0.1
Immutable Struct Updates
(require struct-set) | package: struct-set |
See this link for an explanation of the behavior of struct-copy.
This module defines helpers for immutably updating structs. Notably, updating a struct using this package doesn’t change its type like struct-copy does. Under the hood, it derives a generic method for copying structs that is then dispatched to for updating.
syntax
(struct/set id maybe-super (field ...) struct-option ...)
The same as struct, except the newly defined struct
supports updating via struct-set.
Examples:
> (struct/set fruit (price)) > (struct/set apple fruit (oxidized?)) > (struct/set banana fruit (too-ripe?))
syntax
(struct-set id struct-expr [fld-id expr] ...)
The same as struct-copy, except it will
return the same struct instance as struct-expr,
which may not necessarily be the same as id.
Examples:
> (define old-apple (apple 1.5 #t)) > (define new-apple (struct-set fruit old-apple [price 1.0])) > (fruit-price new-apple) 1.0
> (apple-oxidized? new-apple) #t
syntax
(struct-update id struct-expr [fld-id update-expr] ...)
Like struct-set, except it will apply the result
of update-expr to the old value of the field
to yield the new value.
Examples:
> (define cheap-apple (struct-update fruit old-apple [price sub1])) > (fruit-price cheap-apple) 0.5
> (apple-oxidized? cheap-apple) #t