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 })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.