Import

From HaskellWiki
Revision as of 18:39, 13 February 2007 by BrettGiles (talk | contribs) (typo)

Jump to: navigation, 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 three functions named x, y and z. In that case:

Import command What is brought into scope Notes
import Mod x, y, z, Mod.x, Mod.y, Mod.z (By default, qualified and unqualified names.)
import Mod () (Nothing!) (Useful for only importing instances of typeclasses and nothing else)
import Mod (x,y) x, y, Mod.x, Mod.y (Only x and y, no z.)
import qualified Mod Mod.x, Mod.y, Mod.z (Only qualified versions; no unqualified versions.)
import qualified Mod (x,y) Mod.x, Mod.y (Only x and y, only qualified.)
import Mod hiding (x,y) z, Mod.z (x and y are hidden.)
import qualified Mod hiding (x,y) Mod.z (x and y are hidden.)
import Mod as Foo x, y, z, Foo.x, Foo.y, Foo.z (Unqualified names as before. Qualified names use Foo instead of Mod.)
import Mod as Foo (x,y) x, y, Foo.x, Foo.y (Only import x and y.)
import qualified Mod as Foo Foo.x, Foo.y, Foo.z (Only qualified names, using new qualifier.)