-2
我有以下代碼:沒有實例(民A0)從使用的「它」產生
{-# LANGUAGE OverloadedStrings, FlexibleInstances #-}
type VersionCompound = Maybe Int
class VersionOperations a where
decrement :: a -> a
instance VersionOperations VersionCompound where
decrement Nothing = Nothing
decrement (Just 0) = Just 0
decrement (Just num) = Just (num - 1)
當我嘗試運行命令decrement (Just 5)
,我得到以下錯誤:
<interactive>:8:1:
No instance for (Num a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Num Data.Attoparsec.Internal.Types.Pos
-- Defined in ‘Data.Attoparsec.Internal.Types’
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
instance Num Integer -- Defined in ‘GHC.Num’
...plus five others
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
任何想法如何解決它?
'遞減(只是5):: VersionCompound'應該給它它需要編譯的信息。儘管如此,你應該真的使用'newtype'。 – 4castle
'遞減(Just(5 :: Int))'是使其編譯的另一種方法 – Jogger