[Haskell-cafe] lost in generics

Bas van Dijk v.dijk.bas at gmail.com
Thu Oct 20 23:49:51 CEST 2011

On 20 October 2011 19:12, Rustom Mody <rustompmody at gmail.com> wrote:
> And of course which are easier and which more difficult to dig into.

If you're looking for an example for the new GHC generic mechanism: I
recently added a generic default implementation to the ToJSON and
FromJSON type classes of the aeson package (not in mainline yet):

class ToJSON a where
    toJSON   :: a -> Value

    default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
    toJSON = gToJSON . from

class FromJSON a where
    parseJSON :: Value -> Parser a

    default parseJSON :: (Generic a, GFromJSON (Rep a)) => Value -> Parser a
    parseJSON = fmap to . gParseJSON



It wasn't that difficult to implement. However I did need to use some
advanced type-level tricks to convert to and from records.



More information about the Haskell-Cafe mailing list