[Haskell-beginners] multi-parameter typeclass with default implementation
Twan van Laarhoven
twanvl at gmail.com
Tue Aug 20 14:20:24 CEST 2013
On 20/08/13 12:13, TP wrote:
> {-# LANGUAGE MultiParamTypeClasses #-}
>
> class Bar a where
> bar :: a -> Int
>
> class FooBar a b where
> foobar :: Bar a => a -> b -> Int
> foobar avalue bvalue = bar avalue
>
> instance Bar Int where
> bar i = 5
> instance FooBar Int Int
>
> main = do
> print $ bar (4::Int)
> print $ foobar (5::Int) (2::Int)
It might be better to make Bar a superclass of FooBar,
class Bar a => FooBar a b where
foobar :: a -> b -> Int
foobar a b = bar a
Then the compiler knows that every instance of FooBar also requires an instance
of Bar.
Twan
More information about the Beginners
mailing list