
On 14/06/05, Adde
Cale Gibbard
writes: Perhaps you want data UnitValue u n = (Unit u, Num n) => UnitValue {uUnit :: u, uValue :: n}
I tried adding UnitValue as an instance of class Show, but I can't figure out how to tell the compiler that u is a Unit and m is a Num (shouldn't it be able to figure that out from the above declaration?):
instance Show (UnitValue u m) where show (UnitValue u m) = show m ++ (shortName u)
You indicate the class restrictions just before the name of the class and type, with the usual implication arrow: instance (Num m, Unit u) => Show (UnitValue u m) where show (UnitValue u m) = show m ++ (shortName u)
This is the error I'm getting:
Could not deduce (Show m) from the context (Show (UnitValue u m)) arising from use of `show' at /Users/adde/Projects/Haskell/units.hs:18:26-29 Probable fix: add (Show m) to the class or instance method `show' In the first argument of `(++)', namely `show m' In the definition of `show': show (UnitValue u m) = (show m) ++ (shortName u) In the definition for method `show'
Could not deduce (Unit u) from the context (Show (UnitValue u m)) arising from use of `shortName' at /Users/adde/Projects/Haskell/units.hs:18:37-45 Probable fix: add (Unit u) to the class or instance method `show' In the second argument of `(++)', namely `(shortName u)' In the definition of `show': show (UnitValue u m) = (show m) ++ (shortName u) In the definition for method `show'
When you get errors like this, there are actually two probable fixes: the one mentioned, to add the class restraints to the method (if you're allowed to, in this case you can't/don't want to), or to add the class restraints to the instance, which is what you want to do. - Cale