16.2 Units

A unit contains a set of declarations, procedures and functions that can be used by a program or another unit. The syntax for a unit is as follows:

_________________________________________________________________________________________________________
Units


 --unit -unit-header-interface- part-implementation- part--
---|-----------------------------------end- .---------------------
   -|----------------|---------------|
   |-initialization-part-- - finalization- part --|
   -------- begin---statement-----------
                       ;


--unit-header- unit- dotted- identifier-----------------;-----------------
                                -hint- directives-|


--interface- part-interface-------------------------------------------
                        uses- clause  |-constant- declaration-part--||
                                    |-- type-declaration- part --||
                                    |- vpraroiapbelrte-y d- deeclcalararattioionn--paprartt-||
                                    |--procedure- headers- part-||
                                    |-operator- declaration-part--||
                                    ------------------------|


--procedure- headers- part-procedure- header--;-------------------------
                       function- header       call- modifiers  ;


--                -             --------------            ------
  implementation- part implementation   -uses- clause--|declaration-part


--initialization-part- initialization --|statement --------------------------
                             ----;------


--            -          ---        ----------------------------
  finalization-part  finalization   -statem;ent--|
____________________________________________

As can be seen from the syntax diagram, a unit always consists of a interface and an implementation part. Optionally, there is an initialization block and a finalization block, containing code that will be executed when the program is started, and when the program stops, respectively.

Both the interface part or implementation part can be empty, but the keywords Interface and implementation must be specified. With one exception: in macpas mode, the implementation keyword may be omitted if the implementation is empty.. . The following is a completely valid unit;

unit a;

interface

implementation

end.

The interface part declares all identifiers that must be exported from the unit. This can be constant, type or variable identifiers, and also procedure or function identifier declarations. The interface part cannot contain code that is executed: only declarations are allowed. The following is a valid interface part:

unit a;

interface

uses b;

Function MyFunction : SomeBType;

Implementation

The type SomeBType is defined in unit b.

All functions and methods that are declared in the interface part must be implemented in the implementation part of the unit, except for declarations of external functions or procedures. If a declared method or function is not implemented in the implementation part, the compiler will give an error, for example the following:

unit unita;

interface

Function MyFunction : Integer;

implementation

end.

Will result in the following error:

unita.pp(5,10) Error: Forward declaration not solved "MyFunction:SmallInt;"

The implementation part is primarily intended for the implementation of the functions and procedures declared in the interface part. However, it can also contain declarations of its own: the declarations inside the implementation part are not accessible outside the unit.

The initialization and finalization part of a unit are optional.

The initialization block is used to initialize certain variables or execute code that is necessary for the correct functioning of the unit. The initialization parts of the units are executed in the order that the compiler loaded the units when compiling a program. They are executed before the first statement of the program is executed.

The finalization part of the units are executed in the reverse order of the initialization execution. They are used for instance to clean up any resources allocated in the initialization part of the unit, or during the lifetime of the program. The finalization part is always executed in the case of a normal program termination: whether it is because the final end is reached in the program code or because a Halt instruction was executed somewhere.

In case the program stops during the execution of the initialization blocks of one of the units, only the units that were already initialized will be finalized. Note that in difference with Delphi, in Free Pascal a finalization block can be present without an Initialization block. That means the following will compile in Free Pascal, but not in Delphi.

Finalization
  CleanupUnit;
end.

An initialization section by itself (i. e. without finalization) may simply be replaced by a statement block. That is, the following:

Initialization
  InitializeUnit;
end.

is completely equivalent to

Begin
  InitializeUnit;
end.