加载模块的功能不在范围内

fly*_*ato 6 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
Run Code Online (Sandbox Code Playgroud)

第二个是MessageTypes:

module MessageTypes () where

-- import stuff

import DhtTypes

data PingR = PingR TransactionID Hash20Bytes

-- stuff
Run Code Online (Sandbox Code Playgroud)

这是我加载MessageTypesGHCi 时发生的事情:

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> 
Run Code Online (Sandbox Code Playgroud)

我已经读过ghci没有从文件加载函数Beginning Haskell - 得到"不在范围:数据构造函数"错误,但我仍然找不到答案.

bhe*_*ilr 8

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

module DhtTypes
    ( Bencode(..)
    , TransactionID(..)
    , Hash20Bytes(..)
    ) where
Run Code Online (Sandbox Code Playgroud)

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