[Haskell-cafe] TH instance code.

Edward Kmett ekmett at gmail.com
Tue Jun 22 11:58:22 EDT 2010


What you're looking for is something like:

deriveVariable _t = [d|
    instance Variable $t where
        toVariant = toVariant . show
        fromVariant x = fmap (\v -> read v :: $t) $ fromVariant x|]

deriveVariable (conT ''PageType)
deriveVariable (conT ''Int)
deriveVariable (conT ''Maybe `appT` conT ''Char)
...

On Tue, Jun 22, 2010 at 11:24 AM, Andy Stewart <lazycat.manatee at gmail.com>wrote:

> Hi all,
>
> I have below duplicate code, but i don't know how to use TH instance code.
>
> ------------------------------> duplicate code start
> <------------------------------
> instance Variable PageType where
>    toVariant = toVariant . show
>    fromVariant x = fmap (\v -> read v :: PageType) $ fromVariant x
>
> instance Variable Int where
>    toVariant = toVariant . show
>    fromVariant x = fmap (\v -> read v :: Int) $ fromVariant x
>
> instance Variable (Maybe Char) where
>    toVariant = toVariant . show
>    fromVariant x = fmap (\v -> read v :: Maybe Char) $ fromVariant x
>
> instance Variable (Maybe Int) where
>    toVariant = toVariant . show
>    fromVariant x = fmap (\v -> read v :: Maybe Int) $ fromVariant x
>
> instance Variable ProcessID where
>    toVariant = toVariant . show
>    fromVariant x = fmap (\v -> read v :: ProcessID) $ fromVariant x
>
> instance Variable GWindowId where
>    toVariant = toVariant . show
>    fromVariant x = fmap (\v -> read v :: GWindowId) $ fromVariant x
> ------------------------------> duplicate code end
> <------------------------------
>
> Any TH expert help?
>
> Thanks,
>
>  -- Andy
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100622/ab1708a5/attachment.html


More information about the Haskell-Cafe mailing list