Dynamic variables cookbook
Factor handbook » Factor cookbook

Prev:Control flow cookbook
Next:Vocabularies cookbook


A symbol is a word which pushes itself on the stack when executed. Try it:
SYMBOL: foo foo .
foo

Before using a variable, you must define a symbol for it:
SYMBOL: name

Symbols can be passed to the get and set words to read and write variable values:
"Slava" name set name get print
Slava

If you set variables inside a with-scope, their values will be lost after leaving the scope:
: print-name ( -- ) name get print ; "Slava" name set [ "Diana" name set "There, the name is " write print-name ] with-scope "Here, the name is " write print-name
There, the name is Diana Here, the name is Slava


References
There is a lot more to be said about dynamically-scoped variables and namespaces.
Dynamic variables