[Haskell-cafe] Type Directed Name Resolution
Lauri Alanko
la at iki.fi
Thu Nov 11 08:10:44 EST 2010
On Thu, Nov 11, 2010 at 03:17:39PM +0200, Michael Snoyman wrote:
> data PetOwner
> data FurnitureOwner
>
> data Cat = Cat { owner :: PetOwner }
> data Chair = Chair { owner :: FurnitureOwner }
These are clearly related uses, so as I said, you can use a type class
to overload the accessor name in a controlled fashion.
{-# LANGUAGE EmptyDataDecls, MultiParamTypeClasses, FunctionalDependencies #-}
data PetOwner
data FurnitureOwner
data Cat = Cat { catOwner :: PetOwner }
data Chair = Chair { chairOwner :: FurnitureOwner }
class Owned a b | a -> b where
owner :: a -> b
instance Owned Cat PetOwner where
owner = catOwner
instance Owned Chair FurnitureOwner where
owner = chairOwner
(You can also use associated type families for the same effect.)
Lauri
More information about the Haskell-Cafe
mailing list