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"
25 ARTICLE: "namespaces-global" "Global variables"
35 ARTICLE: "namespaces.private" "Namespace implementation details"
36 "The namestack holds namespaces."
42 "A pair of words push and pop namespaces on the namestack."
48 ARTICLE: "namespaces" "Dynamic variables"
49 "The " { $vocab-link "namespaces" } " vocabulary implements dynamically-scoped variables."
51 "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" } "."
53 "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."
58 "Various utility words provide common variable access patterns:"
61 "namespaces-combinators"
63 "Implementation details your code probably does not care about:"
64 { $subsections "namespaces.private" }
65 "Dynamic variables complement " { $link "locals" } "." ;
70 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value, or " { $link f } } }
71 { $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 } "." } ;
74 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
75 { $description "Assigns a value to the variable in the namespace at the top of the name stack." }
76 { $side-effects "variable" } ;
79 { $values { "variable" "a variable, by convention a symbol" } }
80 { $description "Assigns a value of " { $link f } " to the variable." }
81 { $side-effects "variable" } ;
84 { $values { "variable" "a variable, by convention a symbol" } }
85 { $description "Assigns a value of " { $link t } " to the variable." }
86 { $side-effects "variable" } ;
89 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation "( old -- new )" } } }
90 { $description "Applies the quotation to the old value of the variable, and assigns the resulting value to the variable." }
91 { $side-effects "variable" } ;
94 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation "( old -- new )" } } }
95 { $description "Applies the quotation to the old value of the global variable, and assigns the resulting value to the global variable." }
96 { $side-effects "variable" } ;
100 { "variable" "a variable, by convention a symbol" }
102 { $description "Changes the boolean value of a variable to its opposite." } ;
108 { $description "Runs the quotation in the global namespace." } ;
111 { $values { "n" "a number" } { "variable" "a variable, by convention a symbol" } }
112 { $description "Adds " { $snippet "n" } " to the value of the variable. A variable value of " { $link f } " is interpreted as being zero." }
113 { $side-effects "variable" }
115 { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
119 { $values { "variable" "a variable, by convention a symbol" } }
120 { $description "Increments the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
121 { $side-effects "variable" } ;
124 { $values { "variable" "a variable, by convention a symbol" } }
125 { $description "Decrements the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
126 { $side-effects "variable" } ;
129 { $values { "variable" "a variable, by convention a symbol" } { "n" integer } }
130 { $description "Increments the value of the variable by 1, and returns its new value." }
131 { $notes "This word is useful for generating (somewhat) unique identifiers. For example, the " { $link gensym } " word uses it." }
132 { $side-effects "variable" } ;
135 { $values { "quot" quotation } }
136 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." }
138 { $example "USING: math namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: x" "0 x set" "[ x [ 5 + ] change x get . ] with-scope x get ." "5\n0" }
142 { $values { "value" object } { "key" "a variable, by convention a symbol" } { "quot" quotation } }
143 { $description "Calls the quotation in a new namespace where " { $snippet "key" } " is set to " { $snippet "value" } "." }
144 { $examples "The following two phrases are equivalent:"
145 { $code "[ 3 x set foo ] with-scope" }
146 { $code "3 x [ foo ] with-variable" }
150 { $values { "quot" quotation } { "exemplar" assoc } { "hash" "a new assoc" } }
151 { $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." } ;
154 { $values { "ns" assoc } { "quot" quotation } }
155 { $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" } "." } ;
158 { $values { "namespace" assoc } }
159 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
162 { $values { "g" assoc } }
163 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
166 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
167 { $description "Outputs the value of a variable in the global namespace." } ;
170 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
171 { $description "Assigns a value to the variable in the global namespace." }
172 { $side-effects "variable" } ;
175 { $values { "namestack" "a vector of assocs" } }
176 { $description "Outputs the current name stack." } ;
179 { $values { "namestack" "a vector of assocs" } }
180 { $description "Outputs a copy of the current name stack." } ;
183 { $values { "namestack" "a vector of assocs" } }
184 { $description "Replaces the name stack with a copy of the given vector." } ;
187 { $values { "namespace" assoc } }
188 { $description "Pushes a namespace on the name stack." } ;
191 { $description "Pops a namespace from the name stack." } ;
193 HELP: init-namespaces
194 { $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
198 { $values { "variable" symbol } { "quot" quotation } }
199 { $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." } ;