Import

From HaskellWiki
Revision as of 16:44, 13 February 2007 by MathematicalOrchid (talk | contribs) (I tried to look this up but it wasn't here. So I added it...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The import statement is used to import functions and other definitions from another module. In the simplest case, you just write

import Data.Maybe

to import the named module (in this case Data.Maybe).

However, in more complicated cases, the module can be imported qualified, with or without hiding, and with or without renaming. Getting all of this straight in your head is quite tricky, so here is a table (lifted directly from the language reference manual) that roughly summarises the various possibilities:

Suppose that module Mod exports two functions named x and y. In that case:

Import command What is brought into scope Notes
import Mod x, y, Mod.x, Mod.y
import Mod () (Nothing!) (Why would you do this?)
import Mod (x) x, Mod.x (Only x, no y.)
import qualified Mod Mod.x, Mod.y (Only qualified versions; no unqualified versions.)
import qualified Mod () (Nothing!)
import qualified Mod (x) Mod.x
import Mod hiding () x, y, Mod.x, Mod.y (Same as with no hiding.)
import Mod hiding (x) y, Mod.y (x is hidden.)
import qualified Mod hiding () Mod.x, Mod.y (Same as with no hiding.)
import qualified Mod hiding (x) Mod.y (x is hidden.)
import Mod as Foo x, y, Foo.x, Foo.y (Unqualified names as before. Qualified names use Foo instead of Mod.)
import Mod as Foo (x) x, Foo.x (Only import x.)
import qualified Mod as Foo Foo.x, Foo.y (Only qualified names, using new qualifier.)