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

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

  •  27-06-2022
  •  | 
  •  

Domanda

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.

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top