Difference between revisions of "Import"

From HaskellWiki
Jump to: navigation, search
(I tried to look this up but it wasn't here. So I added it...)
 
m (Made it 3 variables instead of two.)
Line 7: Line 7:
 
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:
 
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 <hask>Mod</hask> exports two functions named <hask>x</hask> and <hask>y</hask>. In that case:
+
Suppose that module <hask>Mod</hask> exports threefunctions named <hask>x</hask>, <hask>y</hask> and <hask>z</hask>. In that case:
   
 
{|
 
{|
Line 16: Line 16:
 
|-
 
|-
 
| <hask>import Mod</hask>
 
| <hask>import Mod</hask>
| <hask>x, y, Mod.x, Mod.y</hask>
+
| <hask>x, y, z, Mod.x, Mod.y, Mod.z</hask>
  +
| (By default, qualified ''and'' unqualified names.)
 
|-
 
|-
 
| <hask>import Mod ()</hask>
 
| <hask>import Mod ()</hask>
Line 24: Line 24:
 
| <hask>import Mod (x)</hask>
 
| <hask>import Mod (x)</hask>
 
| <hask>x, Mod.x</hask>
 
| <hask>x, Mod.x</hask>
| (Only <hask>x</hask>, no <hask>y</hask>.)
+
| (Only <hask>x</hask>, no <hask>y</hask> or <hask>z</hask>.)
 
|-
 
|-
 
| <hask>import qualified Mod</hask>
 
| <hask>import qualified Mod</hask>
| <hask>Mod.x, Mod.y</hask>
+
| <hask>Mod.x, Mod.y, Mod.z</hask>
 
| (Only qualified versions; no unqualified versions.)
 
| (Only qualified versions; no unqualified versions.)
|-
 
| <hask>import qualified Mod ()</hask>
 
| (Nothing!)
 
 
|-
 
|-
 
| <hask>import qualified Mod (x)</hask>
 
| <hask>import qualified Mod (x)</hask>
 
| <hask>Mod.x</hask>
 
| <hask>Mod.x</hask>
  +
| (Only <hask>x</hask>, only qualified.)
 
|-
 
|-
 
| <hask>import Mod hiding ()</hask>
 
| <hask>import Mod hiding ()</hask>
| <hask>x, y, Mod.x, Mod.y</hask>
+
| <hask>x, y, z, Mod.x, Mod.y, Mod.z</hask>
 
| (Same as with no <hask>hiding</hask>.)
 
| (Same as with no <hask>hiding</hask>.)
 
|-
 
|-
 
| <hask>import Mod hiding (x)</hask>
 
| <hask>import Mod hiding (x)</hask>
| <hask>y, Mod.y</hask>
+
| <hask>y, z, Mod.y, Mod.z</hask>
 
| (<hask>x</hask> is hidden.)
 
| (<hask>x</hask> is hidden.)
 
|-
 
|-
 
| <hask>import qualified Mod hiding ()</hask>
 
| <hask>import qualified Mod hiding ()</hask>
| <hask>Mod.x, Mod.y</hask>
+
| <hask>Mod.x, Mod.y, Mod.z</hask>
 
| (Same as with no <hask>hiding</hask>.)
 
| (Same as with no <hask>hiding</hask>.)
 
|-
 
|-
 
| <hask>import qualified Mod hiding (x)</hask>
 
| <hask>import qualified Mod hiding (x)</hask>
| <hask>Mod.y</hask>
+
| <hask>Mod.y, Mod.z</hask>
 
| (<hask>x</hask> is hidden.)
 
| (<hask>x</hask> is hidden.)
 
|-
 
|-
 
| <hask>import Mod as Foo</hask>
 
| <hask>import Mod as Foo</hask>
| <hask>x, y, Foo.x, Foo.y</hask>
+
| <hask>x, y, z, Foo.x, Foo.y, Foo.z</hask>
 
| (Unqualified names as before. Qualified names use <hask>Foo</hask> instead of <hask>Mod</hask>.)
 
| (Unqualified names as before. Qualified names use <hask>Foo</hask> instead of <hask>Mod</hask>.)
 
|-
 
|-
Line 61: Line 59:
 
|-
 
|-
 
| <hask>import qualified Mod as Foo</hask>
 
| <hask>import qualified Mod as Foo</hask>
| <hask>Foo.x, Foo.y</hask>
+
| <hask>Foo.x, Foo.y, Foo.z</hask>
 
| (Only qualified names, using new qualifier.)
 
| (Only qualified names, using new qualifier.)
 
|}
 
|}

Revision as of 16:49, 13 February 2007

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 threefunctions 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!) (Why would you do this?)
import Mod (x) x, Mod.x (Only x, no y or z.)
import qualified Mod Mod.x, Mod.y, Mod.z (Only qualified versions; no unqualified versions.)
import qualified Mod (x) Mod.x (Only x, only qualified.)
import Mod hiding () x, y, z, Mod.x, Mod.y, Mod.z (Same as with no hiding.)
import Mod hiding (x) y, z, Mod.y, Mod.z (x is hidden.)
import qualified Mod hiding () Mod.x, Mod.y, Mod.z (Same as with no hiding.)
import qualified Mod hiding (x) Mod.y, Mod.z (x is 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) x, Foo.x (Only import x.)
import qualified Mod as Foo Foo.x, Foo.y, Foo.z (Only qualified names, using new qualifier.)