purescript - 如何更新用 `data`而不是 `type`定义的PureScript记录?

标签 purescript

更新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/

相关文章:

purescript - 如何在 purescript-halogen 中结合有效的事件处理程序和自定义 EventUpdates?

functional-programming - 你如何在 PureScript 中组合函数?

purescript - purescript 中列表/数组中的类似记录类型

gadt - 数据构造器中的 Rank-2 类型

purescript - psc 已添加到路径中,但 PuLP 仍然找不到 psc

haskell - 功能性思考。在 Haskell/Purescript 中构建新数组

purescript - 我如何编写一个从集合中减去一个效果的 purescript 效果处理程序

javascript - 如何将 PureScript ADT 转换为 FFI 的 JS 字符串 'enum'

haskell - 如何在纯脚本中构建应用程序

purescript - 适当的抽象代替异构(但共享类)列表?