[Haskell-cafe] default function definitions
Malcolm Wallace
malcolm.wallace at me.com
Sun Jul 25 09:44:56 EDT 2010
>> -- Is it true that instances must exists before we can run function
>> or
>> make subclasses?
>> instance C1 Person where
>> instance C1 Employee where
>
> You can *call* class methods only for types which are instances of
> that
> class.
But you can certain *write* functions that make use of the class
methods, even if no instances exist, provided they remain polymorphic
over the class. e.g.
cumulativeAges :: C1 a => [a] -> Integer
cumulativeAges = sum . map age
The assumption would be that some client of your code would eventually
need to declare at least one instance, for some type they are
interested in, before they could use the function at that concrete
type. Those instances need not be in the same module as the
definition of the class, nor in the same module as function
definitions like cumulativeAges.
Regards,
Malcolm
More information about the Haskell-Cafe
mailing list