| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.Vinyl.Generics.Transform
Description
Generic functions to convert plain Haskell records
to their vinyl representation and back.
Documentation
toVinyl :: (IsRecord a rs, NatTrans rs) => a -> Rec ElField rs Source #
Given a plain record, returns the vinyl equivalent with the
field names as type-level strings, tupled with the field type.
Example:
import qualified Generics.SOP as S
import qualified GHC.Generics as G
data MyPlainRecord = MPR {
age :: Int,
iscool :: Bool,
yearbook :: Text
} deriving (Show, G.Generic)
instance S.Generic MyPlainRecord
instance S.HasDatatypeInfo MyPlainRecord
-- Note: requires all 3 instances: G.Generic, S.Generic and S.HasDatatypeInfo
-- this now works! Type signature is optional here.
convertToVinyl :: MyPlainRecord
-> Rec ElField '[("age" ::: Int), ("iscool" ::: Bool), ("yearbook" ::: Text)]
convertToVinyl = toVinyl
fromVinyl :: (Generic a, Code a ~ ts, ts ~ '[ys], Cata rs ys) => Rec ElField rs -> a Source #
Given a vinyl record, returns the plain record equivalent.
Requires the equivalent plain record data declaration to be available
in current scope.
Additionally, it requires explicit type annotation (either using a
type signature or using TypeApplications).
Example:
import qualified Generics.SOP as S
import qualified GHC.Generics as G
r1 :: Rec ElField '[("age" ::: Int), ("iscool" ::: Bool), ("yearbook" ::: Text)]
r1 = xrec (23, True, "!123!")
data MyPlainRecord = MPR {
age :: Int,
iscool :: Bool,
yearbook :: Text
} deriving (Show, G.Generic)
instance S.Generic MyPlainRecord
-- Note: Here we need only G.Generic and S.Generic
-- Using explicit type signature
r2 :: MyPlainRecord
r2 = fromVinyl r1
-- or using TypeApplications
r2' = fromVinyl @MyPlainRecord r1