更新type
定义的记录是可行的,如Differences from Haskell所述
type PointRec = { x :: Number, y :: Number }
setX :: Number -> PointRec -> PointRec
setX val point = point { x = val }
但是当使用
data
定义(并因此指定一个构造函数)时,它不会:data PointRec = PointRec { x :: Number, y :: Number }
setX :: Number -> PointRec -> PointRec
setX val point = point { x = val }
我从编译器得到的错误是
Could not match type
和一些细节。
我在这里可以做什么?
最佳答案
您需要解包和包装数据构造函数:
data PointRec = PointRec { x :: Number, y :: Number }
setX :: Number -> PointRec -> PointRec
setX val (PointRec point) = PointRec (point { x = val })
关于purescript - 如何更新用 `data`而不是 `type`定义的PureScript记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35753717/