Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't import list working in GHC?

Tags:

haskell

The following fails to compile:

module Main where
  import Text.JSON (JSObject, JSValue)

  main = print "hello world"
  getObject :: JSValue -> JSObject JSValue
  getObject (JSObject x) = x

Giving the error:

Not in scope: data constructor `JSObject'

But removing the import list allows it compilation to success succeed (even though JSObject was imported above)

module Main where
  import Text.JSON

  main = print "hello world"
  getObject :: JSValue -> JSObject JSValue
  getObject (JSObject x) = x

Why is GHC (7.4.2) ignoring my import of JSObject?

like image 348
Clinton Avatar asked Jan 14 '23 17:01

Clinton


1 Answers

If you write import Text.JSON (JSObject) you only import type, not the constructors it has. To import constructurs do import Text.JSON (JSObject(..)) or instead of .. specify comma-separated list of constructor names you wish to use, e.g. Text.JSON(JSObject(Cons1, Cons2))

like image 173
Adrian Avatar answered Jan 22 '23 23:01

Adrian