Difference between revisions of "With"

From Lazarus wiki
Jump to navigationJump to search
m (Added back page link)
(Undo revision 133977 by Trev (talk): a “back to <some static page>” does not make sense, as there are many pages referring here)
Tag: Undo
Line 1: Line 1:
{{with}}
+
{{With}}
 
 
 
 
Back to [[Reserved words]].
 
 
 
  
 
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight> allows overriding the scope lookup routing for named scopes for the duration of one [[statement]].
 
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight> allows overriding the scope lookup routing for named scopes for the duration of one [[statement]].
  
== Routing ==
+
== 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
Line 21: Line 15:
 
# the [[System unit|system unit]] (unless implicit inclusion has been disabled)
 
# the [[System unit|system unit]] (unless implicit inclusion has been disabled)
  
== Override ==
+
== override ==
 
+
The lookup order can be temporarily overridden with a <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight>-clause.
The lookup order can be temporarily overridden with a <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight>-clause. It looks like this:
+
It looks like this:
 
 
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
with namedScope do
 
with namedScope do
Line 31: Line 24:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
This puts <syntaxhighlight lang="pascal" enclose="none">namedScope</syntaxhighlight> at the top of the routing.
 
This puts <syntaxhighlight lang="pascal" enclose="none">namedScope</syntaxhighlight> at the top of the routing.
 
Identifiers are looked up in <syntaxhighlight lang="pascal" enclose="none">namedScope</syntaxhighlight> first, before other scopes are considered.
 
Identifiers are looked up in <syntaxhighlight lang="pascal" enclose="none">namedScope</syntaxhighlight> first, before other scopes are considered.
Line 61: Line 53:
 
In practice this will always be a compound statement, though.
 
In practice this will always be a compound statement, though.
  
== See also ==
+
== see also ==
 
 
 
* [[Namespaces|namespaces]]
 
* [[Namespaces|namespaces]]

Revision as of 23:46, 27 August 2020

Template:With

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:

  1. current block
  2. enclosing block, if any
  3. the block enclosing the enclosing block, if any
  4. … (and so on)
  5. the most recently imported module, that means for instance the unit that appears at the end of the uses-clause list, if any
  6. the penultimate module that has been imported, if any
  7. … (and so on)
  8. the first imported module, that means for instance the first unit appearing in a uses-clause, if any
  9. 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 a uses-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 withdo has to be followed by exactly one statement. In practice this will always be a compound statement, though.

see also