Difference between revisions of "Uses"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; deleted category included in page template)
(Undo revision 133871 by Trev (talk); there was no category to be included in the language/categorization template; demote heading levels by one level again (cf. discussion))
Tag: Undo
Line 1: Line 1:
 
{{Uses}}
 
{{Uses}}
 
 
Back to [[Reserved words]].
 
 
 
<syntaxhighlight lang="pascal" enclose="none">Uses</syntaxhighlight> is a [[Reserved word|reserved word]].
 
  
 
The <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause of a Pascal module imports exported [[Identifier|identifiers]] from another module.
 
The <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause of a Pascal module imports exported [[Identifier|identifiers]] from another module.
 
It was introduced by [[UCSD Pascal]] and virtually every modern Pascal [[Compiler|compiler]], including [[FPC]], supports it, if no other mechanism is available.
 
It was introduced by [[UCSD Pascal]] and virtually every modern Pascal [[Compiler|compiler]], including [[FPC]], supports it, if no other mechanism is available.
  
= Usage =
+
<syntaxhighlight lang="pascal" enclose="none">Uses</syntaxhighlight> is a [[Reserved word|reserved word]].
 
 
== Location ==
 
  
 +
== usage ==
 +
=== location ===
 
Every Pascal module – i. e. [[Program|<syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight>]], <syntaxhighlight lang="pascal" enclose="none">unit</syntaxhighlight>, or <syntaxhighlight lang="pascal" enclose="none">library</syntaxhighlight> – can have at most one <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause per section.
 
Every Pascal module – i. e. [[Program|<syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight>]], <syntaxhighlight lang="pascal" enclose="none">unit</syntaxhighlight>, or <syntaxhighlight lang="pascal" enclose="none">library</syntaxhighlight> – can have at most one <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause per section.
 
It has to appear right after the section headings.
 
It has to appear right after the section headings.
Line 20: Line 14:
 
A <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> does not have any explicit section headings, thus the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause appears immediately after the <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> header, but still after any [[global compiler directives]] as some may alter the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause.
 
A <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> does not have any explicit section headings, thus the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause appears immediately after the <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> header, but still after any [[global compiler directives]] as some may alter the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause.
  
== Syntax ==
+
=== syntax ===
 
 
 
The <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause consists of the word <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>, followed by a [[Comma|comma]]-separated list of module names, which is terminated by a [[;|semicolon]].
 
The <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause consists of the word <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>, followed by a [[Comma|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 <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> name may not appear in the list.
 
The modules listed in the clause have to be capable of ''exporting'' identifiers, that means a <syntaxhighlight lang="pascal" enclose="none">program</syntaxhighlight> name may not appear in the list.
 
Usually [[Unit|<syntaxhighlight lang="pascal" enclose="none">unit</syntaxhighlight>]] names are listed in the clause.
 
Usually [[Unit|<syntaxhighlight lang="pascal" enclose="none">unit</syntaxhighlight>]] names are listed in the clause.
 
The module name of the module, that is about to be defined, can not appear in the list.
 
The module name of the module, that is about to be defined, can not appear in the list.
 
 
Example:
 
Example:
 
+
<syntaxhighlight lang="pascal">
<syntaxhighlight lang=pascal>
 
 
uses
 
uses
 
math, sysUtils, baseUnix;
 
math, sysUtils, baseUnix;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Access and shadowing ==
+
=== access and shadowing ===
 
 
 
This makes identifiers exported by the listed modules, for units that means identifiers declared in the <syntaxhighlight lang="pascal" enclose="none">interface</syntaxhighlight> section, known in the current module.
 
This makes identifiers exported by the listed modules, for units that means identifiers declared in the <syntaxhighlight lang="pascal" enclose="none">interface</syntaxhighlight> 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.
 
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.
Line 45: Line 35:
 
A [[With|<syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight>-clause]] may alleviate this situation.
 
A [[With|<syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight>-clause]] may alleviate this situation.
  
== Loading and unloading ==
+
=== loading and unloading ===
 
 
 
At program start, all [[Initialization|<syntaxhighlight lang="pascal" enclose="none">initialization</syntaxhighlight>]] statement blocks, if any, are processed in the order the units were listed in – possibly recursively.
 
At program start, all [[Initialization|<syntaxhighlight lang="pascal" enclose="none">initialization</syntaxhighlight>]] statement blocks, if any, are processed in the order the units were listed in – possibly recursively.
 
At program termination, all [[Finalization|<syntaxhighlight lang="pascal" enclose="none">finalization</syntaxhighlight>]] are processed ''in the reverse order''.
 
At program termination, all [[Finalization|<syntaxhighlight lang="pascal" enclose="none">finalization</syntaxhighlight>]] are processed ''in the reverse order''.
Line 53: Line 42:
 
The main block of a program is not executed at all.
 
The main block of a program is not executed at all.
  
== Special units ==
+
=== special units ===
 
 
 
In FPC the [[System unit|unit <syntaxhighlight lang="pascal" enclose="none">system</syntaxhighlight>]] is implicitly included by every program.
 
In FPC the [[System unit|unit <syntaxhighlight lang="pascal" enclose="none">system</syntaxhighlight>]] is implicitly included by every program.
 
It is wrong to explicitly list it in the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause.
 
It is wrong to explicitly list it in the <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight> clause.
Line 60: Line 48:
 
This switch indicates/ought to indicate, that a/the system unit is about to be compiled.
 
This switch indicates/ought to indicate, that a/the system unit is about to be compiled.
  
= See also =
+
== see also ==
 
 
 
* [[Namespaces|namespaces]]
 
* [[Namespaces|namespaces]]
 
* [https://www.freepascal.org/docs-html/ref/refse108.html § “Unit dependencies” in the ''Free Pascal reference guide'']
 
* [https://www.freepascal.org/docs-html/ref/refse108.html § “Unit dependencies” in the ''Free Pascal reference guide'']

Revision as of 12:07, 21 May 2020

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;

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 accessed be 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.

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