问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/10672981/export-template-haskell-generated-definitions