Can I attach Haddock comments to declarations in a `[d|…|]` declaration quote?

59 Views Asked by At

If I have a declaration quote passed to a Template Haskell function, something like

someTHFunction
  [d|
    -- | Some documentation
    data SomeType = SomeConstructor | SomeOtherConstructor
    |]

is there a way to access the Haddock comment Some documentation inside someTHFunction :: DecsQ -> DecsQ? I tried using getDoc with the SomeType name from inside the DataD, but that fails with

Example.hs:(7,1)-(11,6): error: [GHC-24922] …
    ‘SomeType_0’ is not in scope at a reify

Concretely, suppose someTHFunction is a simple Template Haskell identity function that also calls getDoc:

{-# LANGUAGE TemplateHaskell #-}

module SomeTHModule where

import Language.Haskell.TH

someTHFunction :: DecsQ -> DecsQ
someTHFunction decsq = do
  decs <- decsq
  let names = [name | DataD _ name _ _ _ _ <- decs]
  _ <- traverse (getDoc . DeclDoc) names
  pure decs

If I try to use that in another file, I get the above error:

{-# LANGUAGE TemplateHaskell #-}

module Example where

import SomeTHModule

someTHFunction
  [d|
    -- | Some documentation
    data SomeType = SomeConstructor | SomeOtherConstructor
    |]

{-
Example.hs:(7,1)-(11,6): error: [GHC-24922] …
    ‘SomeType_0’ is not in scope at a reify
-}
0

There are 0 best solutions below