如何消除选择器功能的歧义?
How to disambiguate selector function?
在 GHC 8 中:
{-# LANGUAGE DuplicateRecordFields #-}
data Dog = Dog { name::String }
data Human = Human { name::String }
dog = Dog "Spike"
main = putStrLn $ name dog
此代码无法编译:
Ambiguous occurrence `name'
It could refer to either the field `name', defined at A.hs:4:22
or the field `name', defined at A.hs:3:18
如何正确检索我的狗的名字?
这应该有效:
main = putStrLn $ name (dog :: Dog)
Bare uses of the field refer only to the selector function, and work only if this is unambiguous.
和
However, we do not infer the type of the argument to determine the datatype, or have any way of deferring the choice to the constraint solver.
上面的例子和你的很像:
bad (p :: Person) = personId p
当范围内有另一条带有 personId
字段的记录时,这将不起作用 - 即使它看起来很明显:(
在 GHC 8 中:
{-# LANGUAGE DuplicateRecordFields #-}
data Dog = Dog { name::String }
data Human = Human { name::String }
dog = Dog "Spike"
main = putStrLn $ name dog
此代码无法编译:
Ambiguous occurrence `name'
It could refer to either the field `name', defined at A.hs:4:22
or the field `name', defined at A.hs:3:18
如何正确检索我的狗的名字?
这应该有效:
main = putStrLn $ name (dog :: Dog)
Bare uses of the field refer only to the selector function, and work only if this is unambiguous.
和
However, we do not infer the type of the argument to determine the datatype, or have any way of deferring the choice to the constraint solver.
上面的例子和你的很像:
bad (p :: Person) = personId p
当范围内有另一条带有 personId
字段的记录时,这将不起作用 - 即使它看起来很明显:(