Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update PureScript record defined with `data` instead of `type`?

Tags:

purescript

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?

like image 914
0dB Avatar asked Mar 02 '16 17:03

0dB


1 Answers

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 })
like image 154
Phil Freeman Avatar answered Jan 04 '23 15:01

Phil Freeman