【发布时间】:2011-05-18 18:08:12
【问题描述】:
我正在尝试通过适当的转换将我自己的数据声明为类。我的代码如下所示:
data SomeData = SInteger Integer | SElse deriving Show
class Some a where
toSome :: a -> SomeData
instance Some Int where toSome = SInteger . toInteger
main :: IO ()
main = print $ toSome 3
但是 GHC (7.0.3) 生气并说:
Ambiguous type variable `a0' in the constraints:
(Some a0) arising from a use of `toSome'
at minimal_broken.hs:11:16-21
(Num a0) arising from the literal `3' at minimal_broken.hs:11:23
Probable fix: add a type signature that fixes these type variable(s)
显式类型签名(如 3::Int)解决了这个问题,但非常不方便。
标准的“Show”工作得很好,根据手册,它的声明方式完全相同。
为什么标准的 Show 有效,而我的班级却无效?我错过了什么吗?
P.S.:显式“默认 (Int)”无法解决此问题。
【问题讨论】:
标签: haskell types default typeclass