Given a mkStuff
function that takes a Name
and produces Stuff
using typed TH:
{-# LANGUAGE LambdaCase #-}
module Example where
import Language.Haskell.TH (Code, Name, Q)
import qualified Language.Haskell.TH as TH
data Stuff = Stuff
{ tyName :: String
, isSynonym :: Bool
}
mkStuff :: Name -> Code Q Stuff
mkStuff nm = do
let tyName = TH.nameBase nm
TH.liftCode do
TH.reify nm >>= \case
TH.TyConI (TH.TySynD _nmSyn _tyVars _ty) -> do
TH.examineCode [||
Stuff
{ tyName
, isSynonym = True
}
||]
_other -> do
TH.examineCode [||
Stuff
{ tyName
, isSynonym = False
}
||]
How do I create Name
values to pass to the typed splice? For example, this attempt:
module Demo where
import Scratch
type Foo = Int
x :: Stuff
x = $$(mkStuff ''Foo)
produces:
error:
• Untyped brackets may only appear in untyped splices.
• In the Template Haskell quotation ''Foo
In the typed splice: $$(mkStuff ''Foo)
|
| x = $$(mkStuff ''Foo)
|