简体   繁体   English

GHC拒绝出口合格的模块

[英]GHC refuses to export qualified modules

I want to write a module which re-exports some module it had imported qualified. 我想写一个模块,它重新导出一些已导入的模块。 Something like this: 像这样的东西:

module Foo.A
  ( module Foo.B
  , module Foo.C
  ) where
import qualified Foo.B
import qualified Foo.C

-- bunch of code using Foo.B and Foo.C here

This looks like it ought to work; 这看起来应该有效; however, GHC prints warnings about the exports: 但是,GHC会打印有关出口的警告:

Foo/A.hs:2:5:
    Warning: the export item `module Foo.B' exports nothing

Foo/A.hs:3:5:
    Warning: the export item `module Foo.C' exports nothing

And GHCI refuses to load exports from them. GHCI拒绝加载他们的出口。

I can solve this by making the imports unqualified, but then naming conflicts are likely to arise between those imports and the main module code. 我可以通过使导入不合格来解决这个问题,但是这些导入和主模块代码之间可能会出现命名冲突。

Is there any way to make GHC export these modules? 有没有办法让GHC导出这些模块?

No, that isn't just a limitation of GHC, it's the way import and export is designed to work in Haskell. 不,这不仅仅是GHC的限制,而是导入和导出设计用于Haskell的方式。

A module only has control of its own namespace - it can't affect what people can see from other namespaces. 模块只能控制自己的命名空间 - 它不会影响人们从其他命名空间中看到的内容。 A module "re-export" is just a shorthand to say "export all of the symbols in my own namespace that happen to have been imported here from that other module". 一个模块“重新导出”只是一个简写,说“导出我自己的命名空间中的所有符号,这些符号恰好是从其他模块导入的”。 But symbols that you imported qualified aren't really in your own namespace. 但是您导入限定的符号实际上并不在您自己的命名空间中。

If you want to export two different symbols that have the same name, you won't be able to do it from one module. 如果要导出两个具有相同名称的不同符号,则无法从一个模块执行此操作。 Split the module into two, and export each version from a different module. 将模块拆分为两个,并从另一个模块导出每个版本。

This limitation is also a convenience when the importing module is meant to re-declare some names in the imported module, based on declarations of a qualified import. 当导入模块根据合格导入的声明重新声明导入模块中的某些名称时,此限制也很方便。 For instance: 例如:

module MyPrelude (succ, module Prelude) where

import qualified Prelude as P (succ)
import Prelude hiding (succ)

succ :: ...
succ = ... P.succ ...

this idiom would be really hard to express without a lot of verbosity otherwise. 如果没有很多冗长的话,这个成语真的很难表达。 Also, ask your self "would this make sense without hierarchical modules?". 另外,问问你自己“没有分层模块会有意义吗?”。 If not, then this is very ghc specific, and what is actually going on when referencing a hierarchical module name from an expression is not hierarchical at all. 如果没有,那么这是非常具体的,并且当从表达式引用分层模块名称时实际上发生的事情根本不是分层的。

As to why you are able to reexport individual symbols qualified by some module name you did not import, that seems like a kludge to get heirarchical modules to work heirachically in ghc. 至于为什么你能够重新导出由你没有导入的某个模块名称限定的单个符号,这似乎是让hehrchical模块在ghc中以heirachically工作的kludge。 Gaining Foo.B qualified symbols when importing Foo.A is magical, and I think its because The name Foo is no more an actual heirachical ancestor than Foo.A, but the intended use of exporting things with alternate qualification is in cases where its used from Foo. 在导入Foo时获得Foo.B限定符号.A是神奇的,我认为它是因为Foo这个名字不再是Foo.A的实际的heirachical祖先,但出于具有替代资格的东西的预期用途是在使用它的情况下来自Foo。 I don't think this behavior makes sense at all without GHC extensions, so I am going to take a wild guess that its GHC specific. 如果没有GHC扩展,我认为这种行为根本没有意义,因此我将大胆猜测其GHC的具体情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM