The problem is that Haskell sees extractStreamData
and realizes it's going to return some Colour a => a
, but then you pipe that into show so it also realizes that a
is an instance of Show
, but after that there's no mention of a
.
This means that Haskell can't figure out which type a
should be. The solution is just to help out the typechecker and add some signatures
print $ (extractStreamData someGreys :: [ColourGrey])
like that.
However your typeclass instances are a bit... worrisome. What would happen if you passed you passed a ColourGray
to the ColourRGB
instance? Blowing up at runtime with little to know information is a bad response.
Remember that typeclasses is just a way of doing [really souped up] type based overloading. Really it looks like you want like what Daniel Wagner has or something like
translateRGB :: ColourRGB -> ColourGrey
translateGrey :: ColourGrey -> ColourRGB
And then make your instances more like
instance Colour ColourGrey where
extractStreamData (Greys x) = x
extractStreamData (Colours x) = map translateRGB x
so that you just choose whether you want to treat your stream as ColourGrey
s or ColourRGB
s and work with that. Now when you use extractStreamData
you're not trying to figure out what's in the stream first.