retroforth/doc/book/techniques/lexical-scope

62 lines
1.3 KiB
Text
Raw Normal View History

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