16.3 Namespaces: Dotted Units

As can be seen in the syntax diagram for a unit, a unit name can contain dots. This means that the units can be organized in namespaces.

So, the following is a correct unit declaration:

unit a.b;

interface

Function C : integer;

implementation

Function C : integer;

begin
  Result:=1;
end;

end.

The unit can be used as follows:

program d;

uses a.b;

begin
  Writeln(c);
end.

When resolving symbols, unit scopes always take precedence over symbols inside units.

Given the following units:

unit myunit;

interface

var
  test: record
    a: longint;
  end;

implementation

initialization
  test.a:=2;
end.

and

unit myunit.test;

interface

var
  a: longint;

implementation

initialization
  a:=1;
end.

The following program will resolve myunit.test.a to the variable a in unit myunit.test:

uses
   myunit, myunit.test;

begin
  Writeln('myunit.test.a : ',myunit.test.a);
end.

So it will print:

myunit.test.a : 1

Reversing the order of the units will not change this:

uses
   myunit.test, myunit;

begin
  Writeln('myunit.test.a : ',myunit.test.a);
end.

will also print

myunit.test.a : 1

Similarly, the following program will resolve myunit.test.a to the variable a in unit myunit.test:

uses
   myunit.test, myunit;

begin
  Writeln('a : ',a);
end.

it will print:

a : 1

Similarly, the following program will resolve test.a to the variable test.a in unit myunit:

uses
   myunit.test, myunit;

begin
  Writeln('test.a : ',test.a);
end.

will print

test.a : 2