Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function from loaded module not in scope

I have written two modules. The first one is called 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

The second one is MessageTypes:

module MessageTypes () where

-- import stuff

import DhtTypes

data PingR = PingR TransactionID Hash20Bytes

-- stuff

This is what happen when I load MessageTypes in GHCi:

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> 

I have already read ghci not loading function from file and Beginning Haskell - getting “not in scope: data constructor” error, but I still couldn't find an answer.

like image 552
flying_potato Avatar asked Aug 27 '15 13:08

flying_potato


1 Answers

You're exporting the type Hash20Bytes, but you aren't exporting the constructor Hash20Bytes. You can do so like this

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

The (..) exports all constructors/members of a type/typeclass. You can specify a comma separated list of names if you want to only export specific ones, but usually (..) is what's best in my experience.

like image 171
bheklilr Avatar answered Sep 18 '22 08:09

bheklilr