Why are declaration splices not permitted inside delcaration brackets? Is there a workaround?

StackOverflow https://stackoverflow.com/questions/18582269

  •  27-06-2022
  •  | 
  •  

I'm trying to make a quasiquoter that defines some simple sugar for type declarations. The easiest way to do this is to just use some regular expressions to modify the input text string. But when I compile this code outline code:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}

import Language.Haskell.TH
import Language.Haskell.TH.Quote

myquote = QuasiQuoter 
    { quoteDec = \d -> [d| d |]
    }

GHC generates an error saying:

Declaration splices are not permitted inside declaration brackets

Why is this not allowed? Is there any way to splice declarations? It seems like the only alternative is reparsing the whole input declaration, which would just be obnoxious.

有帮助吗?

解决方案

haskell-src-meta has a parseDecs :: String -> Either String [Dec] to do that parsing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top