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