Haskell:存在类型的记录更新
当我遇到错误时,我试图对现有记录使用记录更新。快速谷歌搜索后,我找到了功能请求#2595,它显示了它的实现GHC 回到版本 6.8.3。我正在使用 6.10.4,所以我认为它应该可以工作,但是功能请求中的示例代码:
{-# LANGUAGE ExistentialQuantification,Rank2Types #-}
module Foo where
data Foo = forall a . Foo { foo :: a -> a, bar :: Int }
x :: Foo
x = Foo { foo = id, bar = 3 }
f :: Foo -> Foo
f rec = rec { foo = id }
g :: Foo -> Foo
g rec = rec { bar = 3 }
产生与功能请求中抱怨的相同的错误:
test.hs:10:8:
Record update for the non-Haskell-98 data type `Foo' is not (yet) supported
Use pattern-matching instead
In the expression: rec {foo = id}
In the definition of `f': f rec = rec {foo = id}
test.hs:13:8:
Record update for the non-Haskell-98 data type `Foo' is not (yet) supported
Use pattern-matching instead
In the expression: rec {bar = 3}
In the definition of `g': g rec = rec {bar = 3}
这是在某个时候有意识地删除的功能,还是应该我提交错误报告?
I was trying to use record update for an existential record when I ran into an error. A quick google led me to feature request #2595, which shows it as implemented for GHC back in version 6.8.3. I'm using 6.10.4, so I'd think it should work, but the example code from the feature request:
{-# LANGUAGE ExistentialQuantification,Rank2Types #-}
module Foo where
data Foo = forall a . Foo { foo :: a -> a, bar :: Int }
x :: Foo
x = Foo { foo = id, bar = 3 }
f :: Foo -> Foo
f rec = rec { foo = id }
g :: Foo -> Foo
g rec = rec { bar = 3 }
yield the same errors as complained of in the feature request:
test.hs:10:8:
Record update for the non-Haskell-98 data type `Foo' is not (yet) supported
Use pattern-matching instead
In the expression: rec {foo = id}
In the definition of `f': f rec = rec {foo = id}
test.hs:13:8:
Record update for the non-Haskell-98 data type `Foo' is not (yet) supported
Use pattern-matching instead
In the expression: rec {bar = 3}
In the definition of `g': g rec = rec {bar = 3}
Was this a consciously dropped feature at some point, or should I file a bug report?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,Trac 单条说它是在版本 6.12 中实现的 - 该错误是在版本 6.8.3 中发现的。所以您使用的版本早于修复程序。
此外,该修复的变更日志条目似乎表明它尚未完全修复;您仍然会收到第一个错误,而不是第二个错误。如果还没有针对其余问题的错误报告,我会说继续提交。
Actually, the Trac slip says it was implemented in version 6.12 — the bug was found in version 6.8.3. So you're using a version that's older than the fix.
Also, the changelog entry for the fix seems to indicate that it's not completely fixed; you'd still be getting the first error, just not the second. If there isn't already a bug report for the rest of the problem, I'd say go ahead and file.
还有一个办法!
如果将数据类型定义从 更改为
,
则编译不会出现错误。 --使用ghc-6.12.2.20100531
There is still another way!
If you change the data type definition from
to
, then it compiles without error. -- using ghc-6.12.2.20100531