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'
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.
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.
Related
I'd like to be able to do something like: import qualified Data.Massiv.Array (qualified map).
This gives error: parse error on input `map'.
Or better yet, import qualified Data.Massiv.Array (qualified map) as AM, so I also can access foo as either foo or AM.foo, unless foo == map, then I have to use AM.map. This is to avoid conflict with Prelude.map.
Write two imports and you can use map as AM.map and use other functions without AM..
import qualified Data.Massiv.Array as AM
import Data.Massiv.Array hiding (map)
Hello i was wondering how do you solve circular dependency in Haskell
I have :
Module for a type.
module Company where
import Tclass
data Worker=Worker{
age::Int,
name::String,
title::Title,
income::Int
}
data Title=Manager | Dev | Tester deriving (Show)
data Company=Company{
cname::String,
people::[Worker],
yf::Int
}
Module for the instance of the typeclass
module Tclass where
import System.Environment
import Company
class Console a where
wLine::a->IO()
rLine::IO String->a
instance Show Worker where
show w="to be defined"
print =putStr . show
If i just want to load the type in ghci and use it ; how do i import them without getting the error:
Module imports form a cycle:
module `Tclass' (.\Tclass.hs)
imports `Company' (Company.hs)
which imports `Tclass' (.\Tclass.hs)
P.S I have tried creating a auxiliary module that imports the two modules and the two modules would import it too,but no luck with this one either :
module Au where
import Tclass
import Company
module Company
import Au
module Tclass
import Au
Moving the Show instance to the other module, you can break the cycle:
module Tclass where
import System.Environment
import Company
class Console a where
wLine::a->IO()
rLine::IO String->a
and
module Company where
data Worker=Worker{
age::Int,
name::String,
title::Title,
income::Int
}
instance Show Worker where
show w="to be defined"
print =putStr . show
data Title=Manager | Dev | Tester deriving (Show)
data Company=Company{
cname::String,
people::[Worker],
yf::Int
}
I am using the yesod-sqlite template and trying to use the get function from Database.Persist in a test.
Here is my code:
[Entity _ task] <- runDB $ selectList [TaskName ==. name] []
...
user <- runDB $ X.get (taskUserId task)
And the error I am getting:
my_project/test/Handler/TaskSpec.hs:47:29: error:
Not in scope: ‘X.get’
No module named ‘X’ is imported.
In the TestImport.hs file, I saw this line:
import Database.Persist as X hiding (get)
To my understanding it should be hidding the get function from the HSpec module, so I could use X.get for database retrieving. I also tried with Database.Persist.get and just get with the same result.
So my doubt is: what that line in TestImport.hs is doing?
The import line is importing everything in the Database.Persist module except get, optionally qualified.
If I'm understanding correctly and you want to import only get qualified, and everything else unqualified, you could use:
import Database.Persist hiding (get)
import qualified Database.Persist as X (get)
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?
You aren't exporting it from your module:
module Test (test_f, Test_Data(..)) where
The (..) part says "export all constructors for TestData".
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.
How can I convert the internal value to a Data.Text.Text?
import qualified Data.Text as T
import qualified Data.Text.Lazy.IO as X
main = do
name <- X.readFile "someFile"
How can I convert the value in name to T.Text?
There's a function explicitly for that, Data.Text.Lazy.toStrict.
I suppose you're actually doing something else in between that requires reading as a lazy Text, otherwise, you should read as a strict Text directly of course.