Copyright | © Jonathan Lorimer 2023 |
---|---|
License | MIT |
Maintainer | jonathanlorimer@pm.me |
Stability | stable |
Safe Haskell | None |
Language | Haskell2010 |
Generic machinery for automatically deriving display instances for record types
Synopsis
- class GDisplay1 f where
- gdisplayBuilder1 :: f p -> Builder
- gdisplayBuilderDefault :: (Generic a, GDisplay1 (Rep a)) => a -> Builder
- newtype RecordInstance a = RecordInstance {
- unDisplayProduct :: a
- type family HasSum f where ...
- class Assert (pred :: Bool) (msg :: ErrorMessage)
- type AssertNoSumRecordInstance (constraint :: Type -> Constraint) a = Assert (Not (HasSum (Rep a))) ((((('Text "\128683 Cannot derive " ':<>: 'ShowType constraint) ':<>: 'Text " instance for ") ':<>: 'ShowType a) ':<>: 'Text " via RecordInstance due to sum type") ':$$: 'Text "\128161 Sum types should use a manual instance or derive one via ShowInstance.")
Documentation
class GDisplay1 f where Source #
Generic typeclass machinery for inducting on the structure
of the type, such that we can thread Display
instances through
the structure of the type. The primary use case is for implementing
RecordInstance
, which does this "threading" for record fields. This
machinery does, crucially, depend on child types (i.e. the type of a
record field) having a Display
instance.
Since: 0.0.5.0
gdisplayBuilder1 :: f p -> Builder Source #
Instances
newtype RecordInstance a Source #
This wrapper allows you to create an Display
instance for a record,
so long as all the record fields have a Display
instance as well.
Example
data Password = Password deriving Display via (OpaqueInstance "[REDACTED]" Password)
data MyRecord = MyRecord { fieldA :: String , fieldB :: Maybe String , fieldC :: Int , pword :: Password } deriving stock (Generic) deriving (Display) via (RecordInstance MyRecord)
putStrLn . Data.Text.unpack . display $ MyRecord "hello" (Just "world") 22 Password
MyRecord { fieldA = hello , fieldB = Just world , fieldC = 22 , pword = [REDACTED] }
Since: 0.0.5.0
Instances
Generic a => Generic (RecordInstance a) Source # | |
Defined in Data.Text.Display.Generic type Rep (RecordInstance a) :: Type -> Type # from :: RecordInstance a -> Rep (RecordInstance a) x # to :: Rep (RecordInstance a) x -> RecordInstance a # | |
(AssertNoSumRecordInstance Display a, Generic a, GDisplay1 (Rep a)) => Display (RecordInstance a) Source # | We leverage the Since: 0.0.5.0 |
Defined in Data.Text.Display.Generic displayBuilder :: RecordInstance a -> Builder Source # displayList :: [RecordInstance a] -> Builder Source # displayPrec :: Int -> RecordInstance a -> Builder Source # | |
type Rep (RecordInstance a) Source # | |
Defined in Data.Text.Display.Generic |
type family HasSum f where ... Source #
This type family is lifted from generic-data. We use it to prevent the user from
deriving a RecordInstance
for sum types
Since: 0.0.5.0
class Assert (pred :: Bool) (msg :: ErrorMessage) Source #
type AssertNoSumRecordInstance (constraint :: Type -> Constraint) a = Assert (Not (HasSum (Rep a))) ((((('Text "\128683 Cannot derive " ':<>: 'ShowType constraint) ':<>: 'Text " instance for ") ':<>: 'ShowType a) ':<>: 'Text " via RecordInstance due to sum type") ':$$: 'Text "\128161 Sum types should use a manual instance or derive one via ShowInstance.") Source #
Constraint to prevent misuse of RecordInstance
deriving via mechanism.
Example
data MySum = A | B | C deriving stock (Generic) deriving (Display) via (RecordInstance MySum)
• 🚫 Cannot derive Display instance for MySum via RecordInstance due to sum type 💡 Sum types should use a manual instance or derive one via ShowInstance. • When deriving the instance for (Display MySum)
Since: 0.0.5.0