retroforth/doc/book/techniques/lexical-scope
crc c8a5b047bd image: rename s:index-of, s:index-of-string, a:index-of, a:index-of-string
FossilOrigin-Name: 88416f7a177a6171739a75db5ad4d399735ecd29836424897629ca1787d776d2
2021-06-04 18:34:59 +00:00

61 lines
1.3 KiB
Text

# Lexical Scope
RETRO has a single dictionary, but does provide a means of using
lexical scope to keep this dictionary clean.
## Example
```
{{
'A var
:++A &A v:inc ;
---reveal---
:B ++A ++A @A n:put nl ;
}}
```
In this example, the lexical namespace is created with `{{`. A
variable (`A`) and word (`++A`) are defined. Then a marker is
set with `---reveal---`. Another word (`B`) is defined, and the
lexical area is closed with `}}`.
The headers between `{{` and `---reveal---` are then hidden from
the dictionary, leaving only the headers between `---reveal---`
and `}}` exposed.
If you wish to hide all headers in a `{{` ... `}}` block, leave
out the `---reveal---`.
```
{{
:a #3 ;
:b a dup * ;
}}
```
## Notes
This only affects word visibility within the scoped area. As an
example:
```
:a #1 ;
{{
:a #2 ;
---reveal---
:b 'a s:evaluate n:put ;
}}
```
In this, after `}}` closes the area, the `:a #2 ;` is hidden and
the `s:evaluate` will find the `:a #1 ;` when `b` is run.
## A Word of Warning
Use of these words can result in a corrupt dictionary and system
crashes. Specifically, use of `---reveal---` with an empty private
or public section will result in dictionary corruption.
If you don't need private words, don't put them in a scope. And if
you don't need public words, don't include the `---reveal---`.