Uses

From Lazarus wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) suomi (fi) français (fr) 日本語 (ja)

The uses clause of a Pascal module imports exported identifiers from another module. It was introduced by UCSD Pascal and virtually every modern Pascal compiler, including FPC, supports it, if no other mechanism is available.

Uses is a reserved word.

usage

location

Every Pascal module – i. e. program, unit, or library – can have at most one uses clause per section. It has to appear right after the section headings. Section headings are interface and implementation in a unit. A program does not have any explicit section headings, thus the uses clause appears immediately after the program header, but still after any global compiler directives as some may alter the uses clause.

syntax

The uses clause consists of the word uses, followed by a comma-separated list of module names, which is terminated by a semicolon. The modules listed in the clause have to be capable of exporting identifiers, that means a program name may not appear in the list. Usually unit names are listed in the clause. The module name of the module, that is about to be defined, can not appear in the list. Example:

uses
	math, sysUtils, baseUnix;

Furthermore, each identifier may be followed by in and a string literal overriding the automatic lookup mechanism. The following will look for a unit named foo in the file bar.pas:

uses
	foo in 'bar.pas';

access and shadowing

This makes identifiers exported by the listed modules, for units that means identifiers declared in the interface section, known in the current module. Either the fully-qualified identifier which is prefixed by the module name, and just the identifier’s stem can be used to refer to the same identifier. For instance, math.ceil as well as just ceil refer to the same function (unless in the current module ceil has been defined otherwise).

However, two or more units listed in the uses clause might declare the same identifier. Then, only the identifier declared by the unit later in the list can be referred to using the short notation. Identifiers declared by earlier units in the uses clause can only be accessed via fully-qualified identifiers. A with-clause may alleviate this situation.

loading and unloading

At program start, all initialization statement blocks, if any, are processed in the order the units were listed in – possibly recursively. At program termination, all finalization are processed in the reverse order.

If initialization fails, only units that have been initialized so far are finalized. The main block of a program is not executed at all. Confer also FPC issue 0036754.

special units

In FPC the unit system is implicitly included by every program. It is wrong to explicitly list it in the uses clause. Inclusion of the system unit can be disabled by using the ‑Us compiler switch. This switch indicates/ought to indicate, that a/the system unit is about to be compiled.

see also