导出模板haskell生成的定义 [英] Export template haskell generated definitions
问题描述
我的模块包含定义,其中的一部分被导出(在 module
子句中)。我想要导出模板Haskell生成的声明。但是由于看起来没有办法用TH修改 module
子句,所以我不能这样做。
有可能指定应该导出TH生成的声明?或者也许还有其他方法可以做到这一点?
您需要导出生成的TH声明的名称。例如,如果您有一个生成数据B = C |的TH函数D
声明,您只需要导出模块Mymodule(B(C,D)),其中...
。
如果您未指定导出列表,则将导出该模块中的所有声明。你可以做的一个小窍门是将所有生成的TH函数放在一个模块中,然后重新导出该模块:
{ - #LANGUAGE TemplateHaskell# - }
- 将所有生成的东西放在一个模块中
模块Bla.Generated其中
generateAFunctionCalledfoo
generateAFunctionCalled bar
- 重新导出生成的模块
模块Bla(模块Bla.Generated)其中
导入限定的Bla.Generated
这有一个缺点,就是你不能把生成的函数放在haddock文档中,但这通常不是你通常所做的。
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?
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.
这篇关于导出模板haskell生成的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!