Reputation: 6039
Is there a way to modify field for a type with multiple constructors?
type U = S | C { xxx : String }
This is not a record, so it has no fields to update!
5| let c = C { xxx = "DD"} in case c of C {} -> { c | xxx = "ZZ" }
^
This `c` value is a:
U
But I need a record!
Upvotes: 0
Views: 190
Reputation: 29106
In type U = S | C { xxx : String }
, xxx
is not a field of U
. It is a field of the record contained in C
. Those are separate types that can be deconstructed into separate values. And the way to do that is to use case
to match the C
constructor and bind the contained value to a name (or further deconstruct it) so we can refer to it on the right side of the ->
.
But then you also need to handle the possibility of c
being S
. What should be returned then? Maybe a default? Or maybe you actually want to return a U
? I've assumed the former here, but the latter would just be construction values of U
as you would anywhere else.
let
c =
C { xxx = "DD" }
in
case c of
C record ->
{ record | xxx = "ZZ" }
S ->
-- What to do here?
{ xxx = "default?" }
Upvotes: 1