]> gitweb.factorcode.org Git - factor.git/blob - core/namespaces/namespaces-docs.factor
ff0542a7b87da8b877c0c7f326033d9d48f6b60f
[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 ;
4 IN: namespaces
5
6 ARTICLE: "namespaces-combinators" "Namespace combinators"
7 { $subsection make-assoc }
8 { $subsection with-scope }
9 { $subsection with-variable }
10 { $subsection bind } ;
11
12 ARTICLE: "namespaces-change" "Changing variable values"
13 { $subsection on }
14 { $subsection off }
15 { $subsection inc }
16 { $subsection dec }
17 { $subsection change } ;
18
19 ARTICLE: "namespaces-global" "Global variables"
20 { $subsection namespace }
21 { $subsection global }
22 { $subsection get-global }
23 { $subsection set-global }
24 { $subsection initialize } ;
25
26 ARTICLE: "namespaces.private" "Namespace implementation details"
27 "The namestack holds namespaces."
28 { $subsection namestack }
29 { $subsection set-namestack }
30 { $subsection namespace }
31 "A pair of words push and pop namespaces on the namestack."
32 { $subsection >n }
33 { $subsection ndrop } ;
34
35 ARTICLE: "namespaces" "Variables and namespaces"
36 "The " { $vocab-link "namespaces" } " vocabulary implements simple dynamically-scoped variables."
37 $nl
38 "A 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 assoc, any object can be used as a variable, however by convention, variables are keyed by symbols (see " { $link "words.symbol" } ")."
39 $nl
40 "The " { $link get } " and " { $link set } " words read and write variable values. The " { $link get } " word searches up 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."
41 { $subsection get }
42 { $subsection set }
43 "Various utility words abstract away common variable access patterns:"
44 { $subsection "namespaces-change" }
45 { $subsection "namespaces-combinators" }
46 { $subsection "namespaces-global" }
47 "Implementation details your code probably does not care about:"
48 { $subsection "namespaces.private" }
49 "An alternative to dynamic scope is lexical scope. Lexically-scoped values and closures are implemented in the " { $vocab-link "locals" } " vocabulary." ;
50
51 ABOUT: "namespaces"
52
53 HELP: get
54 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value, or " { $link f } } }
55 { $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 } "." } ;
56
57 HELP: set
58 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
59 { $description "Assigns a value to the variable in the namespace at the top of the name stack." }
60 { $side-effects "variable" } ;
61
62 HELP: off
63 { $values { "variable" "a variable, by convention a symbol" } }
64 { $description "Assigns a value of " { $link f } " to the variable." }
65 { $side-effects "variable" } ;
66
67 HELP: on
68 { $values { "variable" "a variable, by convention a symbol" } }
69 { $description "Assigns a value of " { $link t } " to the variable." }
70 { $side-effects "variable" } ;
71
72 HELP: change
73 { $values { "variable" "a variable, by convention a symbol" } { "quot" { $quotation "( old -- new )" } } }
74 { $description "Applies the quotation to the old value of the variable, and assigns the resulting value to the variable." }
75 { $side-effects "variable" } ;
76
77 HELP: +@
78 { $values { "n" "a number" } { "variable" "a variable, by convention a symbol" } }
79 { $description "Adds " { $snippet "n" } " to the value of the variable. A variable value of " { $link f } " is interpreted as being zero." }
80 { $side-effects "variable" }
81 { $examples
82     { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
83 } ;
84
85 HELP: inc
86 { $values { "variable" "a variable, by convention a symbol" } }
87 { $description "Increments the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
88 { $side-effects "variable" } ;
89
90 HELP: dec
91 { $values { "variable" "a variable, by convention a symbol" } }
92 { $description "Decrements the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
93 { $side-effects "variable" } ;
94
95 HELP: counter
96 { $values { "variable" "a variable, by convention a symbol" } { "n" integer } }
97 { $description "Increments the value of the variable by 1, and returns its new value." }
98 { $notes "This word is useful for generating (somewhat) unique identifiers. For example, the " { $link gensym } " word uses it." }
99 { $side-effects "variable" } ;
100
101 HELP: with-scope
102 { $values { "quot" quotation } }
103 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." }
104 { $examples
105     { $example "USING: math namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: x" "0 x set" "[ x [ 5 + ] change x get . ] with-scope x get ." "5\n0" }
106 } ;
107
108 HELP: with-variable
109 { $values { "value" object } { "key" "a variable, by convention a symbol" } { "quot" quotation } }
110 { $description "Calls the quotation in a new namespace where " { $snippet "key" } " is set to " { $snippet "value" } "." }
111 { $examples "The following two phrases are equivalent:"
112     { $code "[ 3 x set foo ] with-scope" }
113     { $code "3 x [ foo ] with-variable" }
114 } ;
115
116 HELP: make-assoc
117 { $values { "quot" quotation } { "exemplar" "an assoc" } { "hash" "a new hashtable" } }
118 { $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." } ;
119
120 HELP: bind
121 { $values { "ns" "a hashtable" } { "quot" quotation } }
122 { $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" } "." } ;
123
124 HELP: namespace
125 { $values { "namespace" "an assoc" } }
126 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
127
128 HELP: global
129 { $values { "g" "an assoc" } }
130 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
131
132 HELP: get-global
133 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
134 { $description "Outputs the value of a variable in the global namespace." } ;
135
136 HELP: set-global
137 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
138 { $description "Assigns a value to the variable in the global namespace." }
139 { $side-effects "variable" } ;
140
141 HELP: namestack*
142 { $values { "namestack" "a vector of assocs" } }
143 { $description "Outputs the current name stack." } ;
144
145 HELP: namestack
146 { $values { "namestack" "a vector of assocs" } }
147 { $description "Outputs a copy of the current name stack." } ;
148
149 HELP: set-namestack
150 { $values { "namestack" "a vector of assocs" } }
151 { $description "Replaces the name stack with a copy of the given vector." } ;
152
153 HELP: >n
154 { $values { "namespace" "an assoc" } }
155 { $description "Pushes a namespace on the name stack." } ;
156
157 HELP: ndrop
158 { $description "Pops a namespace from the name stack." } ;
159
160 HELP: init-namespaces
161 { $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
162 $low-level-note ;
163
164 HELP: initialize
165 { $values { "variable" symbol } { "quot" quotation } }
166 { $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." } ;