
18 Aug
2007
18 Aug
'07
4:27 p.m.
Twan van Laarhoven
The solution is to use a dummy parameter:
class IntegerType a where value :: a -> Integer And call it like: f = value (undefined :: Two)
So for instance:
instance IntegerType d => Show (QF d) where show (QF a b) = show a ++ " + " ++ show b ++ " sqrt " ++ show (value (undefined::d))
Thanks to all respondents for this suggestion. That works great.
The problem is that this doesn't work, because d is not in scope, you need the scoped type variables extension:
valueOfQF :: forall a. IntegerType a => QF a -> Integer valueOfQF qf = value (undefined :: a)
Well actually, your first attempt *did* work for me (using GHC 6.6.1). Is this not behaviour that I can rely on?