retroforth/doc/book/techniques/lexical-scope
crc ca23660abe docs: clarify that ---reveal--- can be left out and using it without public names will corrupt memory.
FossilOrigin-Name: ecf434a6642f69195a8637bd44bbdded5fde50e21638f23d576b282c1c079bec
2021-06-03 04:05:58 +00:00

56 lines
1.1 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.
If you have a `---reveal---` with no definitions following, you
will experience memory corruption.