Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

With

Deutsch · English · Suomi · Русский

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. additional automatically loaded units, for example, in a program if enabled, the heapTrc unit (see procedure loaddefaultunits in compiler/pmodules.pas for a full list)
  10. 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:

pascal
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

If multiple with-clauses ought to be nested, there is the short notation:

pascal
with snakeOil, sharpTools do
        begin
            ???
        end;

which is equivalent to:

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

See also


From the Free Pascal wiki, page "With", as the Internet Archive kept it on 2026-01-31, last changed on 2021-12-07. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .