With
From Free Pascal wiki
The reserved word with
allows overriding the scope lookup routing for named scopes for the duration of one statement.
routing
Identifiers are searched in the following order, until there is a hit:
- current block
- enclosing block, if any
- the block enclosing the enclosing block, if any
- … (and so on)
- the most recently imported module, that means for instance the unit that appears at the end of the
uses
-clause list, if any - the penultimate module that has been imported, if any
- … (and so on)
- the first imported module, that means for instance the first unit appearing in a
uses
-clause, if any - the system unit (unless implicit inclusion has been disabled)
override
The lookup order can be temporarily overridden with a with
-clause.
It looks like this:
with namedScope do
begin
…
end;
This puts namedScope
at the top of the routing.
Identifiers are looked up in namedScope
first, before other scopes are considered.
namedScope
may be
- the name of a
unit
that has previously been imported via auses
-clause in the current section - the name of a structured variable, that has named fields, i. e.
If multiple with
-clauses ought to be nested, there is the short notation:
with snakeOil, sharpTools do
begin
…
end;
which is equivalent to:
with snakeOil do
begin
with sharpTools do
begin
…
end;
end;
Note, begin
-end
are not part of the syntax, but with
… do
has to be followed by exactly one statement.
In practice this will always be a compound statement, though.