Can not use Show even if type implements it

  • A+
Category:Languages

I do not understand why i Show typeclass complains on using a custom type since i already provided an instance for it :

Custom Type

data Numeric=I Int | D Double   instance Show Numeric where         show (I x)=show x         show (D y)=show y   instance Num Numeric where         (+) (I a) (I b) =I (a+b)         (+) (D a) (I b) =D (a+ fromIntegral b)         (+) (I a) (D b)=D (fromIntegral a+b)         (-) (D a) (I b)= D (a- fromIntegral b)         (-) (I a) (D b)=D(fromIntegral a -b) 

Method that complains

arrayToString::Num a=>[a]->String arrayToString arr =intercalate "," $ map show arr 

So given my type that implements Num and Show typeclasses i do not understand why it has renders this error when i am feeding arrayToString a [Numeric] value

Error

* Could not deduce (Show a) arising from a use of `show'       from the context: Num a         bound by the type signature for:                    arrayToString :: forall a. Num a => [a] -> String         at Types.hs:40:5-37       Possible fix:         add (Show a) to the context of           the type signature for:             arrayToString :: forall a. Num a => [a] -> Strin 

 


If you want to show something then use the Show constraint:

arrayToString :: (Show a) => [a] -> String 

i do not understand why it has renders this error when i am feeding arrayToString a [Numeric] value

Notice the function arrayToString doesn't have anything specific to Num or Numeric. All it does is render a string for anything Showable (via show).

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: