zerospiel
zerospiel

Reputation: 642

Haskell error Not in scope: data constructor

I wrote some simple module in Haskell and then import it in other file. Then I'm trying to use functions with data constructors from my module — there is an error Not in scope: data constructor: <value>. How can I fix it?

Note: when I'm using it in interpreter after importing — all is good without errors.

My module Test.hs:

module Test (test_f) where
data Test_Data = T|U|F deriving (Show, Eq)

test_f x
    | x == T = T
    | otherwise = F

And my file file.hs:

import Test

some_func = test_f

No error if I'm writing in interpreter:

> :l Test
> test_f T
T

In interpreter I'm trying to execute some_func T, but there is an error. And how can I use class Test_Data in my file to describe annotations?

Upvotes: 11

Views: 4217

Answers (2)

kosmikus
kosmikus

Reputation: 19637

You have an explicit export list in your module Test:

module Test (test_f) where

The export list (test_f) states that you want to export the function test_f and nothing else. In particular, the datatype Test_Data and its constructors are hidden.

To fix this, either remove the export list like this:

module Test where

Now all things will be exported.

Or add the datatype and its constructors to the export list like this:

module Test (test_f, Test_Data(..)) where

The notation Test_Data(..) exports a datatype with all corresponding constructors.

Upvotes: 11

bheklilr
bheklilr

Reputation: 54058

You aren't exporting it from your module:

module Test (test_f, Test_Data(..)) where

The (..) part says "export all constructors for TestData".

Upvotes: 13

Related Questions