8.18
top
← prev up next →

Immutable Struct UpdatesπŸ”— i

Cameron Moy

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 idmaybe-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 applefruit(oxidized?))
> (struct/set bananafruit(too-ripe?))

syntax

( struct-set idstruct-expr[fld-idexpr]...)

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(apple1.5#t))
> (define new-apple(struct-set fruitold-apple[price1.0]))
> (fruit-pricenew-apple)

1.0

> (apple-oxidized?new-apple)

#t

syntax

( struct-update idstruct-expr[fld-idupdate-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 fruitold-apple[pricesub1 ]))
> (fruit-pricecheap-apple)

0.5

> (apple-oxidized?cheap-apple)

#t

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /