Copyright | (C) 2014 Ryan Scott |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Ryan Scott |
Stability | Experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell98 |
deriveShow
deriveShow
automatically generates a Show
instance declaration for a data
type or newtype
. As an example:
{-# LANGUAGE TemplateHaskell #-} import Text.Show.Text.TH (deriveShow) data D a = Nullary | Unary Int | Product String Char a | Record { testOne :: Double , testTwo :: Bool , testThree :: D a } $(deriveShow ''D)
D
now has a Show
instance equivalent to that which would be generated
by a deriving Show
clause.
Note that at the moment, deriveShow
does not support data families,
so it is impossible to use deriveShow
with data instance
s or newtype
instance
s. Also, deriveShow
lacks the ability to properly detect data types
with higher-kinded type parameters (e.g., data HK f a = HK (f a)
), so it cannot
create instances for them either.
deriveShow :: Name -> Q [Dec] Source
Generates a Show
instance declaration for the given data
type or newtype
.
mk
functions
There may be scenarios in which you want to show an arbitrary data
type or newtype
without having to make the type an instance of Show
. For these cases,
Text.Show.Text.TH provide several functions (all prefixed with mk
) that splice
the appropriate lambda expression into your source code.
As an example, suppose you have data ADT = ADTCon
, which is not an instance of Show
.
With mkShow
, you can still convert it to Text
:
{-# LANGUAGE OverloadedStrings, TemplateHaskell #-} whichADT :: Bool whichADT = $(mkShow ''ADT) ADTCon == "ADT"
Note that due the order in which Template Haskell executes splices, the above code
may fail to compile if ADT
is located in the same module and whichADT
. To get
around this, you can use the following hack:
{-# LANGUAGE OverloadedStrings, TemplateHaskell #-} data ADT = ADTCon $(return []) whichADT :: Bool whichADT = $(mkShow ''ADT) ADTCon == "ADT"
mkShow :: Name -> Q Exp Source
Generates a lambda expression which converts the given data
type or newtype
to a strict Text
.
mkShowLazy :: Name -> Q Exp Source
Generates a lambda expression which converts the given data
type or newtype
to a lazy Text
.
mkShowPrec :: Name -> Q Exp Source
Generates a lambda expression which converts the given data
type or newtype
to a strict Text
with the given precedence.
mkShowPrecLazy :: Name -> Q Exp Source
Generates a lambda expression which converts the given data
type or newtype
to a lazy Text
with the given precedence.
mkShowb :: Name -> Q Exp Source
Generates a lambda expression which converts the given data
type or newtype
to a Builder
.
mkShowbPrec :: Name -> Q Exp Source
Generates a lambda expression which converts the given data
type or newtype
to a Builder
with the given precedence.
mkPrint :: Name -> Q Exp Source
Generates a lambda expression which writes the given data
type or newtype
argument's strict Text
output to the standard output, followed by a newline.
mkPrintLazy :: Name -> Q Exp Source
Generates a lambda expression which writes the given data
type or newtype
argument's lazy Text
output to the standard output, followed by a newline.