There are currently 2 (3 if you count TemplateHaskell) options for generic programming using GHC, Data.Data / Data.Typeable and GHC.Generics, both available from the base package. So, what are the advantages and disadvantages of each? Is GHC.Generics the "modern" way and Data.Data obsolete and just kept for backwards compatibility?
The Generic and Generic1 type classesfrom and from1 map values of data types to their generic representations. Rep and Rep1 are associated type functions (the feature is a part of the TypeFamilies GHC extension) that take the type of data we want to manipulate and yield the type of its representation.
Datatype-generic programming, also frequently just called generic programming or generics in Haskell, is a form of abstraction that allows defining functions that can operate on a large class of datatypes.
GHC.Generics is the modern way and it is much faster than SYB. It however exposes a different approach to generic programming to the end user, so I don't think that it should be thought of as a direct replacement of SYB, though it does solve the same problems.
A good example of how those approaches differ from user's perspective can be extracted from the aeson library's functionality of serialization of a record to JSON:
{-# LANGUAGE OverloadedStrings #-} import Data.Aeson data Coord = Coord { x :: Double, y :: Double } instance ToJSON Coord where toJSON (Coord x y) = object ["x" .= x, "y" .= y]
And use toJSON
of ToJSON
typeclass afterwards.
{-# LANGUAGE DeriveGeneric #-} import Data.Aeson import GHC.Generics data Coord = Coord { x :: Double, y :: Double } deriving Generic instance ToJSON Coord
And use the same toJSON
of ToJSON
typeclass afterwards.
{-# LANGUAGE DeriveDataTypeable #-} import Data.Data import Data.Aeson.Generic data Coord = Coord { x :: Double, y :: Double } deriving (Data, Typeable)
And use a specific toJSON
from Data.Aeson.Generic
with the following signature:
toJSON :: Data a => a -> Value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With