4

My module contains definitions, part of which are exported (in module clause). I want to export Template Haskell-generated declarations too. But since there is seemingly no way to modify module clause with TH, I cannot do this.

Is it possible to specify that TH-generated declarations should be exported at all? Or maybe there are other ways to do this?

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296

1 Answers1

7

You need to export the names of the generated TH declarations. For example, if you have a TH function that generates a data B = C | D declaration, you need to simply export module Mymodule (B(C,D)) where ....

If you don't specify an export list, all declarations in that module will be exported. What you can do as a little trick is to put all of your generated TH functions in one module, and then reexport that module:

{-# LANGUAGE TemplateHaskell #-}
-- Put all of the generated stuff in one module
module Bla.Generated where

generateAFunctionCalled "foo"
generateAFunctionCalled "bar"

-- Re-export the generated module
module Bla (module Bla.Generated) where
import qualified Bla.Generated

This has the disadvantage that you can't put haddock documentation for generated functions, but that's not something you usually do anyways.

dflemstr
  • 25,947
  • 5
  • 70
  • 105
  • Problem is that there are a *lot* of names, and they themselves are generated, so enumerating them in export list is not an option. Well, I think I'll stick with export list absence for now. Thank you very much. – Vladimir Matveev May 20 '12 at 12:25
  • Vladimir: If you want to export all the TH definitions, but not other definitions, couldn't you use this solution and `import Bla.Generated hiding (...)` ? That way the things you don't exclude all get exported... – sclv May 20 '12 at 17:04
  • 3
    The point was that the `.Generated` module only contains TH generated stuff. The normal `Bla` module would contain other stuff that you would/maybe would not like to export. – dflemstr May 20 '12 at 18:05