I have a function in Template Haskell
that extracts the type information for sum of record constructors as below:
listFields :: Name -> Q ([[(String,Name,Type)]])
listFields name = do
TyConI (DataD _ _ _ cons _) <- reify name
let showClause (RecC conName fields) = (map (\(x,_,t) -> (nameBase $ x,x,t)) fields)
return $ map showClause cons
Given the type in there for a field, how do you compare equality of that type with a particular type like GHC.Base.String
or Data.Text.Internal.Text
? I see TypeQ
in TH
documentation. It builds type expression. However, I can't find any documentation on how to build a particular type like String
or Text
or Int
so that I can use it for equality comparison? Will appreciate pointers on how to do this, especially how to get the AST for a particular type.
The reason for this question is that given record constructor, we want to convert each field to Text
. However, show
and pack
should be called differently for String
and Text
types. So, need to generate different splices if the type is Text
(no conversion) or String
(only call pack
, don't call show
) or something else (call pack . show
assuming Show
instance exists).