[Haskell-beginners] Typeclasses and "inheritance"

Chaddaï Fouché chaddai.fouche at gmail.com
Fri Jul 24 14:27:35 EDT 2009


On Fri, Jul 24, 2009 at 6:16 PM, Patrick
LeBoutillier<patrick.leboutillier at gmail.com> wrote:
> In the declaration of the class IPAddr, is there any way to force that the
> IPHost and IPMask types are made up from the same IPBits type? Basically I
> would like the compiler to enforce that Word (Host a) and Word (Mask a) be
> the same type for a specific instance of IPAddr.

Unfortunately, this has not been implemented yet (in 6.10), though it
should be in a future version of GHC (pretty soon probably), you'll
then be able to write :

> class (IPHost (Host a), IPMask (Mask a), Word (Host a) ~ Word (Mask a)) => IPAddr a where

but for now you must content yourself with adding it to the function context :

>   -- Takes an IPAddr and returns another one describing the network
>   subnet :: (Word (Host a) ~ Word (Mask a)) => a -> a
>   subnet a = let m = mask a
>                  h = host a
>              in makeIPAddr (fromBits $ (bits h) .&. (bits m)) $ m

Note that I didn't put a IPAddr context since in your code subnet is a
method of this class (with a default implementation), if this wasn't
your intention you should correct the indentation.

-- 
Jedaï


More information about the Beginners mailing list