在 GHC 8.10 和 9.0 版本中,foo (a :: Aaa)
已足以消除我想要使用的字段的歧义。
自从 9.2 版本以后,我现在会收到一个警告(ambiguous-fields):
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版本。