模棱两可的类型变量来自==



我的期望是下面的代码样本应编译

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(,但是如果没有该值,它将很棒。

您的代码仅在conkell中不起作用,仅在ghci中,因为ghci会自动启用ExtendedDefaultRules扩展名。如果您希望在DAML中进行此行为,则需要在文件顶部明确打开{-# LANGUAGE ExtendedDefaultRules #-},然后此代码可行。

但是,就像Haskell一样,我通常不建议打开此扩展名。如果您没有足够的可用类型的情况,则往往很少见,并且此扩展对"交互式类别"的限制会使它变得非常混乱。

最新更新