使用 GHC 8.10 和 9.0
foo (a :: Aaa)
足以消除我想要使用哪个字段的歧义。
从 9.2 开始,我现在收到警告(模糊字段):
The field ‘foo’ belonging to type Aaa is ambiguous.
This will not be supported by -XDuplicateRecordFields in future releases of GHC.
You can use explicit case analysis to resolve the ambiguity.
考虑以下代码片段,请记住
Aaa
和 Bbb
来自外部库,来自同一模块:
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE RecordWildCards #-}
data Aaa = Aaa { foo :: Int }
data Bbb = Bbb { foo :: String }
a :: Aaa
a = Aaa 5
func1 :: Int
func1 = foo (a :: Aaa)
func2 :: Int
func2 = case a of
Aaa{..} -> foo
func3 :: Int
func3 = case a of
a'@Aaa{} -> foo a'
func2
是唯一无警告的解决方案。这真的是目前最好的方法吗?
func3
完全忽略案例分析(这与无法编译的foo a
相同)。
我正在使用9.2.3。
我也遇到过和你一样的情况,最后我发现
OverloadedRecordDot
可以做到这一点。
{-# LANGUAGE OverloadedRecordDot #-}
func4 :: Int
func4 = a.foo