【发布时间】:2018-07-27 01:43:58
【问题描述】:
我有这段代码无法编译。我想了解为什么它不能推断类型。
module Main where
data Combiner a = Combiner a (a -> Int)
comb = Combiner 3 (\x -> 5)
class HasValue a where
getValue :: Int
instance HasValue Combiner where
getValue (Combiner x f) = f x
main = print $ getValue comb
这是错误:
main.hs:8:3: error:
• Could not deduce (HasValue a0)
from the context: HasValue a
bound by the type signature for:
getValue :: HasValue a => Int
at main.hs:8:3-17
The type variable ‘a0’ is ambiguous
• In the ambiguity check for ‘getValue’
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
When checking the class method:
getValue :: forall a. HasValue a => Int
In the class declaration for ‘HasValue’
【问题讨论】:
-
getValue的签名不应该是a -> Int吗? -
这可能不是唯一的错误。有时从第一个开始是有意义的
标签: haskell