0

我的期望是下面的代码示例应该可以编译

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),但如果没有它也能正常工作。

4

1 回答 1

0

您的代码在已编译的 Haskell 中不起作用,仅在 中ghci,因为ghci自动启用ExtendedDefaultRules扩展名。如果您希望在 DAML 中实现此行为,则需要在文件顶部显式打开{-# LANGUAGE ExtendedDefaultRules #-},然后此代码即可工作。

但是,像 Haskell 一样,我通常不建议打开这个扩展。没有足够类型可用的情况往往很少见,并且将此扩展限制为“交互式类”可能会让人感到困惑。

于 2019-06-28T07:55:49.853 回答