[Haskell-cafe] default function definitions
ajs at 2piix.com
Sat Jul 24 14:30:52 EDT 2010
On Jul 24, 2010, at 10:59 AM, Patrick Browne wrote:
> class C1 c1 where
> age :: c1 -> Integer
> -- add default impl, can this be defined only once at class level?
> -- Can this function be redefined in a *class* lower down the
> age(c1) = 1
Yes, but keep in mind that the hierarchy is only two levels tall.
This mechanism isn't meant for OO-style inheritance.
> -- Is it true that instances must exists before we can run function or
> make subclasses?
> instance C1 Person where
> instance C1 Employee where
> -- Is it true that C2 can inherit age, provided an instance of C1
> class C1 c2 => C2 c2 where
> name :: c2 -> String
> name(c2) = "C2"
> instance C2 Person where
> instance C2 Employee where
There's no notion of "inheritance" here. If Person belongs to C2,
then it "must" belong to C1, because you have specifically said that a
C2 needs to be a C1 (presumably because you need a person's age to
compute their name). So Person will be using C1's "age" function, in
virtue of having a C1 instance.
Compare this to:
class C4 c4 where name' :: c4 -> String
instance C1 Person -- gives Person an age function, either default
instance C1 thing => C4 thing -- gives every C1 thing a name, needs
> -- Is it true that C3 cannot override C1 or C2 existing defaults?
> -- Is it true that this must be done at instance level?
> -- class Cx c3 => C3 c3 where
> -- age(c3) = 3
Yes, as I said, the hierarchy is two levels tall.
More information about the Haskell-Cafe