Re: Immutably updating objects
Marc Nieper-WiÃkirchen 31 Oct 2022 10:53 UTC
Am Mo., 31. Okt. 2022 um 11:18 Uhr schrieb siiky <xxxxxx@net-c.cat>:
>
> If I understood correctly, I believe the CHICKEN egg typed-records[0]
> has just that feature.
>
> (defstruct foo x y z)
>
> This defines a type `foo` with fields `x`, `y` & `z`. Other than the
> constructor, getters, and setters, it creates also an `update-foo` that
> works like this:
>
> (let* ((foo (make-foo x: 1 y: 2 z: 3))
> (foo (update-foo foo x: 4)))
> (foo-x foo)) ;=> 4
This is easy to define (in some form syntactically supported by
standard Scheme); however, it is not a general approach for general
record types because it is not type-safe (in the sense of invariants
that may not be preserved).
Conceptually, there is a subclass of all record types, namely those
that basically model a set of independent fields given a type; for
these, the Chicken approach makes sense.