[Haskell-beginners] Typeclasses and "inheritance"
Chaddaï Fouché
chaddai.fouche at gmail.com
Thu Jul 23 16:26:07 EDT 2009
On Thu, Jul 23, 2009 at 10:18 PM, Chaddaï
Fouché<chaddai.fouche at gmail.com> wrote:
> Your instance would look like that :
>
>> instance IPAddr IPv4Addr IPv4Host IPv4Mask where
>> type Host IPv4Addr = IPv4Host
>> type Mask IPv4Addr = IPv4Mask
>> host (IPv4Addr h _) = h
>> mask (IPv4Addr _ m) = m
Oops... I forgot to trim the extra parameters !
> instance IPAddr IPv4Addr where
> type Host IPv4Addr = IPv4Host
> type Mask IPv4Addr = IPv4Mask
> host (IPv4Addr h _) = h
> mask (IPv4Addr _ m) = m
One advantage compared to the multiparameter + functional dependencies
solution is that you can write :
(IPAddr a) => ...
In your context rather than introducing h and m when they're not needed.
If you need to write "Host a" several time in a function, you can put
the following in your context :
(IPAdrr a, Host a ~ h) => ...
and use h for Host a thereafter.
--
Jedaï
More information about the Beginners
mailing list