【发布时间】:2021-08-09 05:25:18
【问题描述】:
上下文
如果我将以下内容加载到支持RecordDotSyntax 的ghc-9.2.1-alpha2 中:
{-# 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 年 8 月 10 日更新
如果我将以下内容添加为 alias 和 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.
【问题讨论】:
-
明确
import Prelude。在存在可重新绑定语法的情况下,GHC 不会隐式导入它。见这里:downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/… -
@alias 看起来这让我更接近了!现在错误少了。添加
import Prelude后,我得到:Not in scope: ‘getField’。我想我需要另一个显式导入? -
getField来自GHC.Records。 -
@alias Gosh,我可以发誓我在上面测试了你的建议,因此接受了答案。但是,现在我在添加
import GHC.Records后再次尝试,我实际上看到了这个:Not in scope: ‘setField’后跟Perhaps you meant ‘getField’ (imported from GHC.Records)。