Not in scope data constructor
Asked Answered
S

2

20

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:

module second () where
    import first 

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

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

Not in scope : data constructor 'S'

Note: There are some functions in each module for sure, I'm just skipping it for brevity

Storytelling answered 20/11, 2012 at 20:47 Comment(0)
F
32
module first () where

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

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

And use as

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(..))

You can also indent the top level,

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.

Fronnia answered 20/11, 2012 at 20:50 Comment(4)
yes, it stars with capital letters(i just forgot to write it here that way) where to write the import line then ?Storytelling
Yes, wouldn't have compiled otherwise.Fronnia
where to write the import First line, so that its data types are in Second's scope ?Storytelling
The imports come directly after the module declaration. The indentation used for them determines the indentation for the module, typically, one would use no indentation for the top-level. So module Second where\n\nimport First\n\n.... (The \n shall be actual newlines)Fronnia
V
3
  • Module names start with a capital - Haskell is case sensitive
  • Line up your code at the left margin - layout is important in Haskell.
  • The bit in brackets is the export list - miss it out if you want to export all the functions, or put everything you want to export in it.

First.hs:

module First where

type S = SetType
data SetType = S[Integer] 

Second.hs:

module Second where
import First
Virtually answered 20/11, 2012 at 20:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.