Reputation: 5949
I am having trouble with HashMap.lookup function. Please give your advise on how to fix following code (I am getting Not in scope: `HashMap.lookup' error):
instance JSON.FromJSON Document where
parseJSON (JSON.Object v) = maybe mzero parser $ HashMap.lookup "document" v
where parser (JSON.Object v') = Document <$> v' JSON..: "name"
<*> v' JSON..: "content"
parser _ = mzero
parseJSON _ = mzero
Upvotes: 1
Views: 220
Reputation: 102009
It depends on how you are importing the package. If you are using something like:
import Data.HashMap
Then you can access the Data.HashMap.lookup
function as simply:
lookup
however you may have a conflict with Prelude.lookup
, in this case you can either add an explicit Prelude
import hiding the lookup
function, i.e. add:
import Prelude hiding (lookup)
at the top of the file, or use the full name:
Data.HashMap.lookup
Note that you cannot simply use HashMap.lookup
. The module is called Data.HashMap
not just HashMap
.
You may want to give an alias to the package, such as:
import Data.HashMap as H
And then use:
H.lookup key hashmap
This will still allow to use non-prefixed version of types/functions if they don't overlap with something else. If you want to be safe you can use a qualified import:
import qualified Data.HashMap as H
In this way everything from the HashMap
module must be prefixed with H.
. Even the types:
-- note: H.HashMap *not* HashMap:
somefunction :: H.HashMap String [Int] -> [Int]
Without qualified
you could still write:
somefunction :: HashMap String [Int] -> [Int]
And you'd have to use H.
only when calling some functions.
Note that there are different packages proving HashMap
. You may want to use Data.HashMap.Lazy
or Data.HashMap.Strict
.
Upvotes: 3
Reputation: 1568
Try to add
import qualified Data.HashMap.Strict as HashMap
on top of your source file.
Upvotes: 3