不在范围数据构造函数中 [英] Not in scope data constructor

查看:169
本文介绍了不在范围数据构造函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two .hs files: one contains a new type declaration, and the other uses it.

first.hs:

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

second.hs:

second.hs:

module second () where
    import first 

当我运行second.hs时,先加载两个模块,再加载第二个模块.

When I run second.hs, both modules first, second are loaded just fine.

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

But, when I write :type S on Haskell platform, the following error appears

不在范围内:数据构造函数"S"

Not in scope : data constructor 'S'

注意:可以肯定的是,每个模块中都有一些功能,为简便起见,我只是跳过了它.

推荐答案

module first () where

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

Assuming in reality the module name starts with an upper case letter, as it must, the empty export list - () - says the module doesn't export anything, so the things defined in First aren't in scope in Second.

完全省略导出列表以导出所有顶级绑定,或在导出列表中列出导出的实体

Completely omit the export list to export all top-level bindings, or list the exported entities in the export list

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

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

(the (..) exports also the constructors of SetType, without that, only the type would be exported).

并用作

module Second where

import First

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

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

or, to limit the imports to specific types and constructors:

module Second where

import First (S, SetType(..))

您还可以缩进顶层,

module Second where

    import First

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

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

but that is ugly, and one can get errors due to miscounting the indentation easily.

这篇关于不在范围数据构造函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆