我有两个.hs文件:一个包含一个新的类型声明,另一个使用它。

first.hs:

module first () where
    type S = SetType
    data SetType = S[Integer]

second.hs:
module second () where
    import first

当我运行second.hs时,两个,第一个,第二个模块都可以正常加载。

但是,当我在Haskell平台上写:type S时,出现以下错误



注意:每个模块中肯定有一些功能,为简便起见,我只是略过

最佳答案

module first () where

假设实际上模块名称必须以大写字母开头,空的导出列表()表示该模块不导出任何内容,因此First中定义的内容不在Second的范围内。

完全省略导出列表以导出所有顶级绑定(bind),或在导出列表中列出导出的实体
module First (S, SetType(..)) where

((..)还会导出SetType的构造函数,否则,只会导出类型)。

并用作
module Second where

import First

foo :: SetType
foo = S [1 .. 10]

或者,将导入限制为特定的类型和构造函数:
module Second where

import First (S, SetType(..))

您也可以缩进顶层,
module Second where

    import First

    foo :: SetType
    foo = S [1 .. 10]

但这很丑陋,而且由于容易错误地计算压痕,会导致错误。

关于haskell - 不在范围数据构造函数中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13481836/

10-10 06:01