]> gitweb.factorcode.org Git - factor.git/blob - core/namespaces/namespaces-docs.factor
05a72c602583ba669031d83b98100d7c0215d5ac
[factor.git] / core / namespaces / namespaces-docs.factor
1 USING: help.markup help.syntax kernel kernel.private
2 sequences words namespaces.private quotations vectors
3 math.parser math words.symbol assocs ;
4 IN: namespaces
5
6 ARTICLE: "namespaces-combinators" "Namespace combinators"
7 { $subsections
8     make-assoc
9     with-scope
10     with-variable
11     bind
12 } ;
13
14 ARTICLE: "namespaces-change" "Changing variable values"
15 { $subsections
16     on
17     off
18     inc
19     dec
20     change
21     change-global
22 } ;
23
24 ARTICLE: "namespaces-global" "Global variables"
25 { $subsections
26     namespace
27     global
28     get-global
29     set-global
30     initialize
31 } ;
32
33 ARTICLE: "namespaces.private" "Namespace implementation details"
34 "The namestack holds namespaces."
35 { $subsections
36     namestack
37     set-namestack
38     namespace
39 }
40 "A pair of words push and pop namespaces on the namestack."
41 { $subsections
42     >n
43     ndrop
44 } ;
45
46 ARTICLE: "namespaces" "Dynamic variables"
47 "The " { $vocab-link "namespaces" } " vocabulary implements dynamically-scoped variables."
48 $nl
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" } "."
50 $nl
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."
52 { $subsections
53     get
54     set
55 }
56 "Various utility words provide common variable access patterns:"
57 { $subsections
58     "namespaces-change"
59     "namespaces-combinators"
60 }
61 "Implementation details your code probably does not care about:"
62 { $subsections "namespaces.private" }
63 "Dynamic variables complement " { $link "locals" } "." ;
64
65 ABOUT: "namespaces"
66
67 HELP: get
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 } "." } ;
70
71 HELP: set
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" } ;
75
76 HELP: off
77 { $values { "variable" "a variable, by convention a symbol" } }
78 { $description "Assigns a value of " { $link f } " to the variable." }
79 { $side-effects "variable" } ;
80
81 HELP: on
82 { $values { "variable" "a variable, by convention a symbol" } }
83 { $description "Assigns a value of " { $link t } " to the variable." }
84 { $side-effects "variable" } ;
85
86 HELP: change
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" } ;
90
91 HELP: change-global
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" } ;
95
96 HELP: +@
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" }
100 { $examples
101     { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
102 } ;
103
104 HELP: inc
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" } ;
108
109 HELP: dec
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" } ;
113
114 HELP: counter
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" } ;
119
120 HELP: with-scope
121 { $values { "quot" quotation } }
122 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." }
123 { $examples
124     { $example "USING: math namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: x" "0 x set" "[ x [ 5 + ] change x get . ] with-scope x get ." "5\n0" }
125 } ;
126
127 HELP: with-variable
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" }
133 } ;
134
135 HELP: make-assoc
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." } ;
138
139 HELP: bind
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" } "." } ;
142
143 HELP: namespace
144 { $values { "namespace" assoc } }
145 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
146
147 HELP: global
148 { $values { "g" assoc } }
149 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
150
151 HELP: get-global
152 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
153 { $description "Outputs the value of a variable in the global namespace." } ;
154
155 HELP: set-global
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" } ;
159
160 HELP: namestack*
161 { $values { "namestack" "a vector of assocs" } }
162 { $description "Outputs the current name stack." } ;
163
164 HELP: namestack
165 { $values { "namestack" "a vector of assocs" } }
166 { $description "Outputs a copy of the current name stack." } ;
167
168 HELP: set-namestack
169 { $values { "namestack" "a vector of assocs" } }
170 { $description "Replaces the name stack with a copy of the given vector." } ;
171
172 HELP: >n
173 { $values { "namespace" assoc } }
174 { $description "Pushes a namespace on the name stack." } ;
175
176 HELP: ndrop
177 { $description "Pops a namespace from the name stack." } ;
178
179 HELP: init-namespaces
180 { $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
181 $low-level-note ;
182
183 HELP: initialize
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." } ;