Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Successfully imported Data Constructor not in scope?

What is going on here? I'm importing a data constructor, apparently successfully because I don't get an error, but when I try to use the constructor, I get an error saying its not in scope!

In Test.hs:

import Database.Persist (Key)

main = Key

Result:

$ ghc test.hs
[1 of 1] Compiling Main             ( test.hs, test.o )

test.hs:3:8: Not in scope: data constructor `Key'
like image 812
Drew Avatar asked Dec 30 '13 07:12

Drew


2 Answers

import Database.Persist (Key)

The above imports a type named Key but none of its constructors. To import the constructor Key of the type Key you need to do

import Database.Persist (Key(Key))

or just

import Database.Persist (Key(..))

to import all constructors of the given type.

like image 191
shang Avatar answered Sep 28 '22 18:09

shang


In order to import a constructor you must use the following syntax

import Database.Persist (Key (..))

Generally, when importing a type or typeclass by name only the type gets imported. The constructors and member functions must be imported using the Name (..) or Name (Constructor) syntax. This is fairly convenient as it's often the case that you need to write a type signature using an imported type even if you don't ever need to construct or examine values of that type.

like image 32
J. Abrahamson Avatar answered Sep 28 '22 16:09

J. Abrahamson