Import declaration

Grammar§

ImportDeclaration ::= Attribute* "import" (Type ",")* Type ","? ";"

Semantics§

An import gives access to the declarations located in foreign units. Each foreign unit is specified using a Type which must be a TypeIdentifier and that matches to the UnitDeclaration of the foreign unit.

unit a;
unit b;
import a;

An import creates a regular scoped symbol that can be optionally used to fully qualify the foreign symbols.

unit a;
alias B = u8;
unit b;
import a;
var B b1;
var a.B b2; // if "unit b" declares a "B", distinction is possible

In the same way, imports declared in an import can be fully qualified

unit a;
alias B = u8;
unit b;
import a;
unit c;
import b;
var b.a.B b1;

but only if they are public.

unit a;
alias B = u8;
unit b;
protection (private) import a;
unit c;
import b;
var b.a.B b1; // error, "a" not visible

Because imports create regular symbols, other declarations can conflict with them

unit b;
import a;
var s32 a; // error, "a" already declared
unit b;
var s32 a;
import a; // error, "a" already declared

These symbols take for name the first identifier of the chain.

unit a;
import b.c; // adds a symbol "b" to the unit scope.

The symbols matching to the root of an import dont conflict between themselves

unit b;
import a.b, a.c; // "a" can be redeclared, it is always the same root unit
var s32 a;       // error, "a" already declared

They dont create conflict even if located directly in the matching root

unit a;
import a.b;

The relationship between root and child units only exists in the symbols created for the imports, not directly in the units.

This is for example why this

unit a;
unit a.b;
alias B = u8;
unit unrelated;
import a;       // does not leak "a.b" declarations.
alias C = B;    // error, cannot solve type "B"

or that

unit a;
import b; // error, because the link to "b" does not exist in "a" scope.
unit a.b;

does not compile.