With: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: With
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The [[Reserved word|reserved word]] `with` allows overriding the scope lookup routing for named scopes for the duration of one [[statement]]. | |
| 2 | + | ||
| 3 | + | ## Routing | |
| 4 | + | ||
| 5 | + | [[Identifier|Identifiers]] are searched in the following order, until there is a hit: | |
| 6 | + | ||
| 7 | + | 1. current [[Block|block]] | |
| 8 | + | 2. enclosing block, if any | |
| 9 | + | 3. the block enclosing the enclosing block, if any | |
| 10 | + | 4. … (and so on) | |
| 11 | + | 5. the most recently imported module, that means for instance the [[Unit|unit]] that appears at the end of the [[Uses|uses-clause]] list, if any | |
| 12 | + | 6. the penultimate module that has been imported, if any | |
| 13 | + | 7. … (and so on) | |
| 14 | + | 8. the first imported module, that means for instance the first unit appearing in a `uses`-clause, if any | |
| 15 | + | 9. additional automatically loaded units, for example, in a [[Program|program]] if enabled, the [[heaptrc|heapTrc unit]] (see procedure [`loaddefaultunits` in `compiler/pmodules.pas`](https://gitlab.com/freepascal.org/fpc/source/-/tree/release_3_2_0/compiler/pmodules.pas#L318-L412) for a full list) | |
| 16 | + | 10. the [[System unit|system unit]] (unless implicit inclusion has been disabled) | |
| 17 | + | ||
| 18 | + | ## Override | |
| 19 | + | ||
| 20 | + | The lookup order can be temporarily overridden with a `with`-clause. It looks like this: | |
| 21 | + | ||
| 22 | + | ``` pascal | |
| 23 | + | with namedScope do | |
| 24 | + | begin | |
| 25 | + | … | |
| 26 | + | end; | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | This puts `namedScope` at the top of the routing. Identifiers are looked up in `namedScope` first, before other scopes are considered. | |
| 30 | + | ||
| 31 | + | `namedScope` may be | |
| 32 | + | ||
| 33 | + | - the name of a [[Unit|unit]] that has previously been imported via a [[Uses|uses-clause]] in the current section | |
| 34 | + | - the name of a structured variable, that could have named members, i. e. | |
| 35 | + | - a [[Record|record]] | |
| 36 | + | - [[Object|object]], or | |
| 37 | + | - [[Class|class]]. | |
| 38 | + | ||
| 39 | + | If multiple `with`-clauses ought to be nested, there is the short notation: | |
| 40 | + | ||
| 41 | + | ``` pascal | |
| 42 | + | with snakeOil, sharpTools do | |
| 43 | + | begin | |
| 44 | + | … | |
| 45 | + | end; | |
| 46 | + | ``` | |
| 47 | + | ||
| 48 | + | which is equivalent to: | |
| 49 | + | ||
| 50 | + | ``` pascal | |
| 51 | + | with snakeOil do | |
| 52 | + | begin | |
| 53 | + | with sharpTools do | |
| 54 | + | begin | |
| 55 | + | … | |
| 56 | + | end; | |
| 57 | + | end; | |
| 58 | + | ``` | |
| 59 | + | ||
| 60 | + | Note, [[Begin|begin]]-[[End|end]] are not part of the syntax, but `with` … [[Do|do]] has to be followed by exactly *one* statement. In practice this will always be a compound statement, though. | |
| 61 | + | ||
| 62 | + | ## See also | |
| 63 | + | ||
| 64 | + | - [[Namespaces|namespaces]] | |
| 65 | + | ||
| 66 | + | --- | |
| 67 | + | *From the Free Pascal wiki, page "With", as it stood on 2021-12-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. 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](/wiki-contact).* |
