class - 导入 haskell 模块说 "not in scope"

标签 class haskell instance

我在名为 Tree2.hs 的文件中创建了一个树结构

module Tree2
(
 Tree
) where

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)

然后我将其导入并尝试将其用作类的实例

import qualified Tree2

class YesNo a where
  yesno :: a -> Bool

instance YesNo (Tree2.Tree a) where
  yesno EmptyTree = False
  yesno _ = True

但是我在 ghci 中加载它时遇到了这个错误:

Not in scope: data constructor ‘EmptyTree’
Failed, modules loaded: Tree2.

有人知道为什么吗?

最佳答案

首先,

module Tree2
(
 Tree
) where

仅导出Tree 数据类型,不导出其构造函数;你应该使用

module Tree2
(
  Tree(..)
) where

相反。

其次,当您进行合格导入时,您需要使用 Tree2.EmptyTree 而不仅仅是 EmptyTree

关于class - 导入 haskell 模块说 "not in scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30831956/

相关文章:

haskell - 无法将预期类型 `()' 与实际类型 `Int' 匹配

c# - 将一个对象分配给同一类的另一个对象

python - 如何将带有 2 个参数的类字典复制到 2 个列表中

python - 可以从嵌套类定义实现继承吗?

javascript - 如何在WP中找到特定的类

parsing - 如何在 monad 转换器解析器组合器中限制回溯

c# - 如何调用顶级构造函数?

rest - postWith 是否切换了我的请求的 Content-Type?

python - 误解 web.py 应用程序

java - Java servlet 实例预计可以持续多长时间?同一实例是否为所有客户端提供服务?可以有多个实例吗?