import

Summary

Imports a module.

Syntax

import moduleName;

Parameters

moduleName
The fully-qualified name of the module to import.

Description

The import keyword is used to import a module (declared with the module keyword) and its members.

Imported modules are scoped to the file's top-level scope.

Full Qualification and Partial Qualification

Types (classes, etc.) do not need to be fully qualified if they are direct children of the imported module. A fully-qualified name (FQN) is an unambiguous name that completely includes all of the names in the hierarchical sequence leading to the symbol and ending with the symbol itself; in simpler terms, the fully-qualified name is the absolute path to an object, and a partially-qualified name is the relative path.

When module members are imported via the import keyword, they may not need to be fully qualified. For example, if we declare the following module(s):

1
2
3
4
5
6
module Foo.Bar.Baz
{
    class Qux
    {
    }
}

If we import Foo.Bar.Baz, we can access Qux relative to the imported path (Foo.Bar.Baz):

1
2
3
4
import Foo.Bar.Baz;
 
Qux qux1 = new Qux(); // partial qualification is ok
Foo.Bar.Baz.Qux qux2 = new Foo.Bar.Baz.Qux(); // full qualification is also ok

In the above example, when the identifier Qux is encountered, the compiler will try to locate it relative to the imported path(s). Therefore, if we only import Foo.Bar, we cannot access Qux without further qualification:

1
2
3
4
5
import Foo.Bar;
 
Qux qux1 = new Qux(); // error!
Baz.Qux qux2 = new Baz.Qux(); // partial qualification is ok
Foo.Bar.Baz.Qux qux3 = new Foo.Bar.Baz.Qux(); // full qualification is also ok

Collisions and Conflicts

Since full qualification is not necessary for imported module members, it is possible for collisions or conflicts to occur.

For instance, consider the following:

1
2
3
4
5
6
module Foo.Bar.Baz
{
    class Baz
    {
    }
}

If we try to import this module, we may run into a potential conflict:

1
import Foo.Bar.Baz;

In the above example, Baz would point to the class Foo.Bar.Baz.Baz. If you want to access the module Baz, you would need to at least partially qualify via Bar.Baz or fully qualify with Foo.Bar.Baz. Therefore, this conflict is actually automatically resolved by the compiler.Unimplemented

Ambiguity

In another instance, we may have a conflict that cannot be resolved by the compiler.

Consider the following example:

1
2
module Foo { class Baz {} }
module Bar { class Baz {} }

If we try to import both of the above modules, we would try:

1
2
3
4
import Foo;
import Bar;
 
Baz baz = new Baz(); // What does 'Baz' point to?

In this case, Baz can point to either Foo.Baz or Bar.Baz. Due to the ambiguity, the compiler will raise an error demanding you to resolve the ambiguity by fully qualifying.Unimplemented

In order to resolve the ambiguity, we simply just have to fully qualify the Baz we are pointing to:

1
2
3
4
5
import Foo;
import Bar;
 
Foo.Baz baz1 = new Foo.Baz();
Bar.Baz baz2 = new Bar.Baz();

See Also

Share

HTML | BBCode | Direct Link