2012-11-20 59 views
13

我有两个名为.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时,出现以下错误

Not in scope : data constructor 'S' 

注:有每个模块可以肯定在某些功能,我只是跳过它 的问题作出澄清

回答

17
module first() where 

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

完全省去导出列表导出所有顶级绑定,或列出导出的实体导出列表

module First (S, SetType(..)) where 

(在(..)出口也SetType的构造函数,如果没有,只有类型会被出口)。

与应用,

module Second where 

import First 

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

您也可以缩进顶层,

module Second where 

    import First 

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

但那是丑陋的,人们可以得到应有的轻松计数错误的缩进错误。

+0

是的,它颗星的大写字母(我只是忘了在这里写这种方式) 哪里写导入行呢? – Shimaa

+0

是的,否则不会编译。 –

+0

哪里写导入第一行,这样它的数据类型在Second的范围内? – Shimaa

2
  • 模块的名字开始与资本 - Haskell是区分大小写
  • 行了你的代码在左旁 - 布局是非常重要的在Haskell。
  • 括号中的位是导出列表 - 如果要导出所有函数或将所有要导出的函数放在其中,则将其忽略。

First.hs

module First where 

type S = SetType 
data SetType = S[Integer] 

Second.hs

module Second where 
import First 
相关问题