1 USING: help.markup help.syntax kernel kernel.private
2 sequences words namespaces.private quotations vectors
3 math.parser math words.symbol assocs ;
6 ARTICLE: "namespaces-combinators" "Namespace combinators"
14 ARTICLE: "namespaces-change" "Changing variable values"
24 ARTICLE: "namespaces-global" "Global variables"
33 ARTICLE: "namespaces.private" "Namespace implementation details"
34 "The namestack holds namespaces."
40 "A pair of words push and pop namespaces on the namestack."
46 ARTICLE: "namespaces" "Dynamic variables"
47 "The " { $vocab-link "namespaces" } " vocabulary implements dynamically-scoped variables."
49 "A dynamic variable is an entry in an assoc of bindings, where the assoc is implicit rather than passed on the stack. These assocs are termed " { $emphasis "namespaces" } ". Nesting of scopes is implemented with a search order on namespaces, defined by a " { $emphasis "namestack" } ". Since namespaces are just assocs, any object can be used as a variable. By convention, variables are keyed by " { $link "words.symbol" } "."
51 "The " { $link get } " and " { $link set } " words read and write variable values. The " { $link get } " word searches the chain of nested namespaces, while " { $link set } " always sets variable values in the current namespace only. Namespaces are dynamically scoped; when a quotation is called from a nested scope, any words called by the quotation also execute in that scope."
56 "Various utility words provide common variable access patterns:"
59 "namespaces-combinators"
61 "Implementation details your code probably does not care about:"
62 { $subsections "namespaces.private" }
63 "Dynamic variables complement " { $link "locals" } "." ;
68 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value, or " { $link f } } }
69 { $description "Searches the name stack for a namespace containing the variable, and outputs the associated value. If no such namespace is found, outputs " { $link f } "." } ;
72 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
73 { $description "Assigns a value to the variable in the namespace at the top of the name stack." }
74 { $side-effects "variable" } ;
77 { $values { "variable" "a variable, by convention a symbol" } }
78 { $description "Assigns a value of " { $link f } " to the variable." }
79 { $side-effects "variable" } ;
82 { $values { "variable" "a variable, by convention a symbol" } }
83 { $description "Assigns a value of " { $link t } " to the variable." }
84 { $side-effects "variable" } ;
87 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation "( old -- new )" } } }
88 { $description "Applies the quotation to the old value of the variable, and assigns the resulting value to the variable." }
89 { $side-effects "variable" } ;
92 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation "( old -- new )" } } }
93 { $description "Applies the quotation to the old value of the global variable, and assigns the resulting value to the global variable." }
94 { $side-effects "variable" } ;
97 { $values { "n" "a number" } { "variable" "a variable, by convention a symbol" } }
98 { $description "Adds " { $snippet "n" } " to the value of the variable. A variable value of " { $link f } " is interpreted as being zero." }
99 { $side-effects "variable" }
101 { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
105 { $values { "variable" "a variable, by convention a symbol" } }
106 { $description "Increments the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
107 { $side-effects "variable" } ;
110 { $values { "variable" "a variable, by convention a symbol" } }
111 { $description "Decrements the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
112 { $side-effects "variable" } ;
115 { $values { "variable" "a variable, by convention a symbol" } { "n" integer } }
116 { $description "Increments the value of the variable by 1, and returns its new value." }
117 { $notes "This word is useful for generating (somewhat) unique identifiers. For example, the " { $link gensym } " word uses it." }
118 { $side-effects "variable" } ;
121 { $values { "quot" quotation } }
122 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." }
124 { $example "USING: math namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: x" "0 x set" "[ x [ 5 + ] change x get . ] with-scope x get ." "5\n0" }
128 { $values { "value" object } { "key" "a variable, by convention a symbol" } { "quot" quotation } }
129 { $description "Calls the quotation in a new namespace where " { $snippet "key" } " is set to " { $snippet "value" } "." }
130 { $examples "The following two phrases are equivalent:"
131 { $code "[ 3 x set foo ] with-scope" }
132 { $code "3 x [ foo ] with-variable" }
136 { $values { "quot" quotation } { "exemplar" assoc } { "hash" "a new assoc" } }
137 { $description "Calls the quotation in a new namespace of the same type as " { $snippet "exemplar" } ", and outputs this namespace when the quotation returns. Useful for quickly building assocs." } ;
140 { $values { "ns" assoc } { "quot" quotation } }
141 { $description "Calls the quotation in the dynamic scope of " { $snippet "ns" } ". When variables are looked up by the quotation, " { $snippet "ns" } " is checked first, and setting variables in the quotation stores them in " { $snippet "ns" } "." } ;
144 { $values { "namespace" assoc } }
145 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
148 { $values { "g" assoc } }
149 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
152 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
153 { $description "Outputs the value of a variable in the global namespace." } ;
156 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
157 { $description "Assigns a value to the variable in the global namespace." }
158 { $side-effects "variable" } ;
161 { $values { "namestack" "a vector of assocs" } }
162 { $description "Outputs the current name stack." } ;
165 { $values { "namestack" "a vector of assocs" } }
166 { $description "Outputs a copy of the current name stack." } ;
169 { $values { "namestack" "a vector of assocs" } }
170 { $description "Replaces the name stack with a copy of the given vector." } ;
173 { $values { "namespace" assoc } }
174 { $description "Pushes a namespace on the name stack." } ;
177 { $description "Pops a namespace from the name stack." } ;
179 HELP: init-namespaces
180 { $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
184 { $values { "variable" symbol } { "quot" quotation } }
185 { $description "If " { $snippet "variable" } " does not have a value in the global namespace, calls " { $snippet "quot" } " and assigns the result to " { $snippet "variable" } " in the global namespace." } ;