Support spliced function names in type signatures in TH declaration quotes
There doesn't seem to be a way to splice function names into type signatures in Template Haskell declaration quotes [d|...|]
. For example, fDecl1
below does not work.
According to this StackOverflow answer, the approach in fDecl2
below used to work, but it doesn't work with recent GHCs and is much less readable than fDecl1
.
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
fName :: Name
fName = mkName "f"
fTy :: TypeQ
fTy = [t| Int |]
fBody :: ExpQ
fBody = [e| 3 |]
-- | Not allowed:
--
-- @
-- error:
-- Invalid type signature: $fName :: ...
-- Should be of form <variable> :: <type>
-- @
--
-- Similarly, using @$(varP fName) :: $fTy@ fails with an analogous
-- error.
fDecl1 :: DecsQ
fDecl1 = [d| $fName :: $fTy
$(varP fName) = $fBody |]
-- | Not allowed:
--
-- @
-- error:
-- Splices within declaration brackets not (yet) handled by Template Haskell
-- @
fDecl2 :: DecsQ
fDecl2 = [d| $((:[]) <$> sigD fName fTy)
$(varP fName) = $fBody |]
Trac metadata
Trac field | Value |
---|---|
Version | 8.4.3 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | #11129 (closed) |
Blocking | |
CC | |
Operating system | |
Architecture |