上下文
如果我将以下内容加载到ghc-9.2.1-alpha2中支持 RecordDotSyntax :
{-# LANGUAGE OverloadedRecordDot, OverloadedRecordUpdate, DuplicateRecordFields #-}
----------------------------------------------------------------------
data Point = Point { x :: Double, y :: Double }
instance Show Point where
show p = "Point { x = " ++ show p.x ++ ", y = " ++ show p.y ++ " }"
p = Point 10 20
然后我可以在 ghci 中运行以下命令:
ghci> p { x = 30 }
Point { x = 30.0, y = 20.0 }
太棒了,它正在工作!
问题
但是,如果我将以下内容添加到上面的测试文件中:
result =
let
a = Point 1 2
b = a { x = 3 }
in
b
然后重新加载,我收到以下消息:
ghci> :r
[1 of 1] Compiling Main ( /home/dharmatech/tmp/test-ghc-9.2.0.20210422/point-update-issue.hs, interpreted )
/home/dharmatech/tmp/test-ghc-9.2.0.20210422/point-update-issue.hs:13:13: error:
RebindableSyntax is required if OverloadedRecordUpdate is enabled.
|
13 | b = a { x = 3 }
| ^^^^^^^^^^^
Failed, no modules loaded.
我尝试过的
如果我按照消息提示添加 RebindableSyntax
,我会收到更多如下错误:
/home/dharmatech/tmp/test-ghc-9.2.0.20210422/point-update-issue.hs:3:27: error:
Not in scope: type constructor or class ‘Double’
|
3 | data Point = Point { x :: Double, y :: Double }
|
问题
有办法让它发挥作用吗?或者只是还没有实现?
更新 2021-08-10
如果我添加以下内容作为别名和Ari建议:
import Prelude
import GHC.Records
我得到以下信息:
point-update-issue.hs:17:13: error:
Not in scope: ‘setField’
Perhaps you meant ‘getField’ (imported from GHC.Records)
|
17 | b = a { x = 3 }
| ^^^^^^^^^^^
Failed, no modules loaded.
最佳答案
这里有两个问题。
您已启用
RebindableSyntax
。这允许您重新定义某些行为 通过在本地范围内定义某些函数(否则从 Prelude 导入)。因此,RebindableSyntax
意味着NoImplicitPrelude
。您需要手动导入Prelude
,可以选择隐藏要覆盖的函数。import Prelude hiding (...)
您已启用
OverloadedRecordUpdate
,这是 GHC 9.2 中的一项实验性功能,尚未稳定。相关版本GHC User's Guide有这样说:
At this time, RebindableSyntax must be enabled when OverloadedRecordUpdate is and users are required to provide definitions for getField and setField. We anticipate this restriction to be lifted in a future release of GHC with builtin support for setField.
默认的
getField
可以从GHC.Records
导入,但是setField
目前不可用,所以你必须自己实现。玩得开心!
关于haskell - 使用 RecordDotSyntax 更新记录会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68707198/