container for different types, avoiding boiler plate

Markus.Schnell@infineon.com Markus.Schnell@infineon.com
Thu, 21 Aug 2003 10:58:28 +0200


After taking a look at Data.Dynamic I came up with:

> data Gender = Masc | Fem | Neutr     deriving (Typeable, Show)
> data Number = First | Second | Third deriving (Typeable, Show)
>
> type Attrs = [Dynamic]
>
> attrs = [toDyn Masc, toDyn Second]
>
> gattr :: (Typeable a) => Attrs -> Maybe a                   
> gattr []     = Nothing
> gattr (x:xs) = case fromDynamic x of
>                  Nothing -> gattr xs
>                  Just x  -> Just x
>
> type GetAttr a = Attrs -> Maybe a
> ggender = gattr :: GetAttr Gender
> gnumber = gattr :: GetAttr Number

which already is much more readable. But I'm sure
problems are just around the corner ...

Thank you.
Markus