haskell - 加载模块中的函数不在范围内

标签 haskell module ghci

我写了两个模块。第一个称为 DhtTypes:

module DhtTypes (Bencode, encode, TransactionID, Hash20Bytes) where

-- import stuff

class Bencode a where
    encode :: a -> ByteString.ByteString

data TransactionID = TransactionID Int.Int16
data Hash20Bytes = Hash20Bytes [Word.Word8]

-- stuff

第二个是MessageTypes:

module MessageTypes () where

-- import stuff

import DhtTypes

data PingR = PingR TransactionID Hash20Bytes

-- stuff

这就是我在 GHCi 中加载 MessageTypes 时发生的情况:

GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 2] Compiling DhtTypes         ( DhtTypes.hs, interpreted )
[2 of 2] Compiling MessageTypes     ( /home/{path}/MessageTypes.hs, interpreted )
Ok, modules loaded: MessageTypes, DhtTypes.
*MessageTypes> :browse DhtTypes
class Bencode a where
  encode :: a -> ByteString.ByteString
data TransactionID = DhtTypes.TransactionID GHC.Int.Int16
data Hash20Bytes = DhtTypes.Hash20Bytes [GHC.Word.Word8]
*MessageTypes> Hash20Bytes

<interactive>:3:1: Not in scope: data constructor `Hash20Bytes'
*MessageTypes> :l DhtTypes
[1 of 1] Compiling DhtTypes         ( DhtTypes.hs, interpreted )
Ok, modules loaded: DhtTypes.
*DhtTypes> Hash20Bytes [0..10]
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package bytestring-builder-0.10.6.0.0 ... linking ... done.
0123456789a
*DhtTypes> 

我已经阅读了 ghci not loading function from fileBeginning Haskell - getting “not in scope: data constructor” error ,但我仍然找不到答案。

最佳答案

您正在导出类型 Hash20Bytes,但没有导出构造函数 Hash20Bytes。你可以这样做

module DhtTypes
    ( Bencode(..)
    , TransactionID(..)
    , Hash20Bytes(..)
    ) where

(..) 导出类型/类型类的所有构造函数/成员。如果您只想导出特定名称,则可以指定以逗号分隔的名称列表,但根据我的经验,通常 (..) 是最好的。

关于haskell - 加载模块中的函数不在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250989/

相关文章:

haskell - 为什么 haskell 编译器可以推断出这种类型,而 ghci 不能?

haskell - 试图在 ghci 输出中放置一些命令

haskell - 如果 cabal 或工作目录设置为项目目录,Emacs Interactive-Haskell repl 无响应

haskell - Haskell Servant从处理程序获取当前路由/URL

javascript - Uncaught Error : [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:unpr] Unknown provider: $routeProvider

javascript - 创建 Meteor 包库?

node.js - NodeJS 中的提示模块重复输入

haskell - 用高阶函数代替显式递归

haskell - 尽管安装了依赖项,但在尝试配置 Setup.hs 时出现依赖项问题

haskell - 得到低于 x 的素数