Next: 9.5 Libraries
Up: 9. Programs, units, blocks
Previous: 9.3 Blocks
Subsections
Identifiers are valid from the point of their declaration until the end of
the block in which the declaration occurred. The range where the identifier
is known is the scope of the identifier. The exact scope of an
identifier depends on the way it was defined.
The scope of a variable declared in the declaration part of a block,
is valid from the point of declaration until the end of the block.
If a block contains a second block, in which the identfier is
redeclared, then inside this block, the second declaration will be valid.
Upon leaving the inner block, the first declaration is valid again.
Consider the following example:
Program Demo;
Var X : Real;
{ X is real variable }
Procedure NewDeclaration
Var X : Integer; { Redeclare X as integer}
begin
// X := 1.234; {would give an error when trying to compile}
X := 10; { Correct assigment}
end;
{ From here on, X is Real again}
begin
X := 2.468;
end.
In this example, inside the procedure, X denotes an integer variable.
It has it's own storage space, independent of the variable X outside
the procedure.
The field identifiers inside a record definition are valid in the following
places:
- to the end of the record definition.
- field designators of a variable of the given record type.
- identifiers inside a With statement that operates on a variable
of the given record type.
A component identifier is valid in the following places:
- From the point of declaration to the end of the class definition.
- In all descendent types of this class.
- In all method declaration blocks of this class and descendent classes.
- In a with statement that operators on a variable of the given class's
definition.
Note that method designators are also considered identifiers.
All identifiers in the interface part of a unit are valid from the point of
declaration, until the end of the unit. Furthermore, the identifiers are
known in programs or units that have the unit in their uses clause.
Identifiers from indirectly dependent units are not available.
Identifiers declared in the implementation part of a unit are valid from the
point of declaration to the end of the unit.
The system unit is automatically used in all units and programs.
It's identifiers are therefore always known, in each program or unit
you make.
The rules of unit scope implie that you can redefine an identifier of a
unit. To have access to an identifier of another unit that was redeclared in
the current unit, precede it with that other units name, as in the following
example:
unit unitA;
interface
Type
MyType = Real;
implementation
end.
Program prog;
Uses UnitA;
{ Redeclaration of MyType}
Type MyType = Integer;
Var A : Mytype; { Will be Integer }
B : UnitA.MyType { Will be real }
begin
end.
This is especially useful if you redeclare the system unit's identifiers.
root
1999-06-10