Updating a record defined with type
works, as explained over at Differences from Haskell
type PointRec = { x :: Number, y :: Number }
setX :: Number -> PointRec -> PointRec
setX val point = point { x = val }
but when defined with data
(and thus specifying a constructor), it doesn’t:
data PointRec = PointRec { x :: Number, y :: Number }
setX :: Number -> PointRec -> PointRec
setX val point = point { x = val }
The error I get from the compiler is
Could not match type
and some details.
What can I do here?
You need to unwrap and wrap the data constructor:
data PointRec = PointRec { x :: Number, y :: Number }
setX :: Number -> PointRec -> PointRec
setX val (PointRec point) = PointRec (point { x = val })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With