Difference between revisions of "With"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{With}} The reserved word <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight> allows a shortened spelling of records. It is on...")
 
(trying to determine where there is spam)
Line 1: Line 1:
 
{{With}}
 
{{With}}
  
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight> allows a shortened spelling of [[Record|records]]. It is only used in conjunction with the [[Do|<syntaxhighlight lang="pascal" enclose="none">do</syntaxhighlight>]] reserved word.
+
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.
  
Example:
+
== routing ==
<syntaxhighlight>
+
[[Identifier]]s are searched in the following order, until there is a hit:
// Definition of the record
+
# current [[Block|block]]
type
+
# enclosing block, if any
  TreRecord = record
+
# the block enclosing the enclosing block, if any
    strValue: string;
+
# … (and so on)
    intValue: integer;
+
# the most recently imported module, that means for instance the [[Unit|unit]] that appears at the end of the [[Uses|<syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>-clause]] list, if any
    dblValue: double;
+
# the penultimate module that has been imported, if any
  end;
+
# … (and so on)
 +
# the first imported module, that means for instance the first unit appearing in a <syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>-clause, if any
 +
# the [[System unit|system unit]] (unless implicit inclusion has been disabled)
  
var
+
== override ==
  reRecord: TreRecord; // Create the record
+
The lookup order can be temporarily overridden with a <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight>-clause.
 +
It looks like this:
 +
<syntaxhighlight lang="pascal">
 +
with namedScope do
 +
begin
 +
 +
end;
 +
</syntaxhighlight>
 +
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.
  
begin
+
<syntaxhighlight lang="pascal" enclose="none">namedScope</syntaxhighlight> may be
  ...
+
* the name of a [[Unit|<syntaxhighlight lang="pascal" enclose="none">unit</syntaxhighlight>]] that has previously been imported via a [[Uses|<syntaxhighlight lang="pascal" enclose="none">uses</syntaxhighlight>-clause]] in the current section
 +
* the name of a structured variable, that has named fields, i. e.
 +
** a [[Record|<syntaxhighlight lang="pascal" enclose="none">record</syntaxhighlight>]]
 +
** [[Object|<syntaxhighlight lang="pascal" enclose="none">object</syntaxhighlight>]], or
 +
** [[Class|<syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>]].
  
  // standard notation:
+
If multiple <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight>-clauses ought to be nested, there is the short notation:
  reRecord.strValue := 'Test';
+
<syntaxhighlight lang="pascal">
  reRecord.intValue := 5;
+
with snakeOil, sharpTools do
  reRecord.dblValue := 4.2;
+
begin
 +
 +
end;
 +
</syntaxhighlight>which is equivalent to:<syntaxhighlight lang="pascal">
 +
with snakeOil do
 +
begin
 +
with sharpTools do
 +
begin
 +
 +
end;
 +
end;
 +
</syntaxhighlight>
  
  // When used with
+
Note, [[Begin|<syntaxhighlight lang="pascal" enclose="none">begin</syntaxhighlight>]]-[[End|<syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight>]] are not part of the syntax, but <syntaxhighlight lang="pascal" enclose="none">with</syntaxhighlight> … [[Do|<syntaxhighlight lang="pascal" enclose="none">do</syntaxhighlight>]] has to be followed by exactly ''one'' statement.
  with reRecord do
+
In practice this will always be a compound statement, though.
  begin
 
    strValue := 'Test';
 
    intValue := 5;
 
    dblValue := 4.2;
 
  end;
 
  ...
 
end;
 
</syntaxhighlight>
 

Revision as of 13:07, 22 August 2019

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