我的期望是下面的代码示例应该可以编译
testOptionalEq = None == None
testEitherEq = Left 1 == Left 1
testOptionalShow = show None
testEitherShow = show (Left 1)
然而每一行都会导致编译错误,要么
Ambiguous type variable `a1' arising from a use of `=='
prevents the constraint `(Eq a1)' from being solved.
Probable fix: use a type annotation to specify what `a1' should be.
或者
Ambiguous type variable `a0' arising from a use of `show'
prevents the constraint `(Show a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
ghci
在作品中尝试类似的 Haskell 代码。一种解决方法是为值提供显式类型签名(例如None : Optional Int
),但如果没有它也能正常工作。