Haskell 重新导出时命名空间

Haskell namespace when reexporting

我不确定名称空间在以下情况下如何工作:

Data.Streaming.Network reexports HostPreference from Data.Streaming.Network.Internal

当我在 ghci 中加载时,我可以看到构造函数以原始命名空间为前缀。

$ ghci                                                          
GHCi, version 7.8.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Data.Streaming.Network
Prelude Data.Streaming.Network> :info HostPreference
data HostPreference
  = Data.Streaming.Network.Internal.HostAny
  | Data.Streaming.Network.Internal.HostIPv4
  | Data.Streaming.Network.Internal.HostIPv4Only
  | Data.Streaming.Network.Internal.HostIPv6
  | Data.Streaming.Network.Internal.HostIPv6Only
  | Data.Streaming.Network.Internal.Host String
        -- Defined in ‘Data.Streaming.Network.Internal’

但有些程序希望构造函数的绑定在再导出模块下可用 Data.Streaming.Network。

Prelude Data.Streaming.Network> :l src/Server.hs
[1 of 1] Compiling Main             ( src/Server.hs, interpreted )

src/Server.hs:14:58: Not in scope: data constructor ‘HostAny’

这是怎么回事,谁说的对?

HostPreference 的构造函数未导出,streaming-commons 包打算将它们设为私有,如果我要根据导出的内容来判断的话。相反,文档建议使用 OverloadedStrings 并键入字符串文字来获取不同的值:

data HostPreference

Which host to bind.

Note: The IsString instance recognizes the following special values:

* means HostAny

*4 means HostIPv4

!4 means HostIPv4Only

*6 means HostIPv6

!6 means HostIPv6Only

Any other values is treated as a hostname. As an example, to bind to the IPv4 local host only, use "127.0.0.1".

因此,在您的 Server.hs 中,只需启用 OverloadedStrings,然后在您尝试使用 HostAny 的任何地方,只需键入 "*"