Difference between revisions of "With"
(trying to determine where there is spam) |
(→Routing: update source code link) |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{with}} |
− | The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" | + | The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>with</syntaxhighlight> allows overriding the scope lookup routing for named scopes for the duration of one [[statement]]. |
+ | |||
+ | == Routing == | ||
− | |||
[[Identifier]]s are searched in the following order, until there is a hit: | [[Identifier]]s are searched in the following order, until there is a hit: | ||
+ | |||
# current [[Block|block]] | # current [[Block|block]] | ||
# enclosing block, if any | # enclosing block, if any | ||
# the block enclosing the enclosing block, if any | # the block enclosing the enclosing block, if any | ||
# … (and so on) | # … (and so on) | ||
− | # the most recently imported module, that means for instance the [[Unit|unit]] that appears at the end of the [[Uses|<syntaxhighlight lang="pascal" | + | # the most recently imported module, that means for instance the [[Unit|unit]] that appears at the end of the [[Uses|<syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>-clause]] list, if any |
# the penultimate module that has been imported, if any | # the penultimate module that has been imported, if any | ||
# … (and so on) | # … (and so on) | ||
− | # the first imported module, that means for instance the first unit appearing in a <syntaxhighlight lang="pascal" | + | # the first imported module, that means for instance the first unit appearing in a <syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>-clause, if any |
+ | # additional automatically loaded units, for example, in a [[Program|<syntaxhighlight lang="pascal" inline>program</syntaxhighlight>]] if enabled, the [[heaptrc|<syntaxhighlight lang="pascal" inline>heapTrc</syntaxhighlight> unit]] (see procedure {{gitlab|repository|FPC|release_3_2_0/compiler/pmodules.pas#L318-L412|<syntaxhighlight lang="pascal" inline>loaddefaultunits</syntaxhighlight> in <syntaxhighlight lang="text" inline>compiler/pmodules.pas</syntaxhighlight>}} for a full list) | ||
# the [[System unit|system unit]] (unless implicit inclusion has been disabled) | # the [[System unit|system unit]] (unless implicit inclusion has been disabled) | ||
− | == | + | == Override == |
− | The lookup order can be temporarily overridden with a <syntaxhighlight lang="pascal" | + | |
− | It looks like this: | + | The lookup order can be temporarily overridden with a <syntaxhighlight lang="pascal" inline>with</syntaxhighlight>-clause. It looks like this: |
+ | |||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
with namedScope do | with namedScope do | ||
Line 24: | Line 28: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | <syntaxhighlight lang="pascal" | + | This puts <syntaxhighlight lang="pascal" inline>namedScope</syntaxhighlight> at the top of the routing. |
− | + | Identifiers are looked up in <syntaxhighlight lang="pascal" inline>namedScope</syntaxhighlight> first, before other scopes are considered. | |
− | |||
− | |||
− | |||
− | |||
− | + | <syntaxhighlight lang="pascal" inline>namedScope</syntaxhighlight> may be | |
+ | * the name of a [[Unit|<syntaxhighlight lang="pascal" inline>unit</syntaxhighlight>]] that has previously been imported via a [[Uses|<syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>-clause]] in the current section | ||
+ | * the name of a structured variable, that could have named members, i. e. | ||
+ | ** a [[Record|<syntaxhighlight lang="pascal" inline>record</syntaxhighlight>]] | ||
+ | ** [[Object|<syntaxhighlight lang="pascal" inline>object</syntaxhighlight>]], or | ||
+ | ** [[Class|<syntaxhighlight lang="pascal" inline>class</syntaxhighlight>]]. | ||
+ | |||
+ | If multiple <syntaxhighlight lang="pascal" inline>with</syntaxhighlight>-clauses ought to be nested, there is the short notation: | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
with snakeOil, sharpTools do | with snakeOil, sharpTools do | ||
Line 50: | Line 55: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Note, [[Begin|<syntaxhighlight lang="pascal" | + | Note, [[Begin|<syntaxhighlight lang="pascal" inline>begin</syntaxhighlight>]]-[[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] are not part of the syntax, but <syntaxhighlight lang="pascal" inline>with</syntaxhighlight> … [[Do|<syntaxhighlight lang="pascal" inline>do</syntaxhighlight>]] has to be followed by exactly ''one'' statement. |
In practice this will always be a compound statement, though. | In practice this will always be a compound statement, though. | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | * [[Namespaces|namespaces]] |
Latest revision as of 01:35, 7 December 2021
│
Deutsch (de) │
English (en) │
suomi (fi) │
русский (ru) │
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 - additional automatically loaded units, for example, in a
program
if enabled, theheapTrc
unit (see procedureloaddefaultunits
incompiler/pmodules.pas
for a full list) - 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 could have named members, 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.