我想用適當的轉換類聲明我自己的數據。我的代碼如下所示:Haskell Num類和我的類似節目之間的歧義
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 ::智力)修復該問題,但它是非常不方便。
標準的「顯示」工作得很好,根據手冊它的聲明完全一樣。
爲什麼標準顯示作品,但我的班級沒有?我錯過了什麼?
P.S .:明確的「default(Int)」不能解決這個問題。