]> gitweb.factorcode.org Git - factor.git/blob - core/slots/slots-docs.factor
Fix permission bits
[factor.git] / core / slots / slots-docs.factor
1 USING: help.markup help.syntax generic kernel.private parser
2 words kernel quotations namespaces sequences words arrays
3 effects generic.standard classes.builtin
4 slots.private classes strings math assocs byte-arrays alien
5 math classes.tuple ;
6 IN: slots
7
8 ARTICLE: "accessors" "Slot accessors"
9 "For every tuple slot, a " { $emphasis "reader" } " method is defined in the " { $vocab-link "accessors" } " vocabulary. The reader is named " { $snippet { $emphasis "slot" } ">>" } " and given a tuple, pushes the slot value on the stack."
10 $nl
11 "Writable slots - that is, those not attributed " { $link read-only } " - also have a " { $emphasis "writer" } ". The writer is named " { $snippet "(>>" { $emphasis "slot" } ")" } " and stores a value into a slot. It has stack effect " { $snippet "( value object -- )" } ". If the slot is specialized to a specific class, the writer checks that the value being written into the slot is an instance of that class first. See " { $link "tuple-declarations" } " for details."
12 $nl
13 "In addition, two utility words are defined for each writable slot."
14 $nl
15 "The " { $emphasis "setter" } " is named " { $snippet ">>" { $emphasis "slot" } } " and stores a value into a slot. It has stack effect " { $snippet "( object value -- object )" } "."
16 $nl
17 "The " { $emphasis "changer" } " is named " { $snippet "change-" { $emphasis "slot" } } ". It applies a quotation to the current slot value and stores the result back in the slot; it has stack effect " { $snippet "( object quot -- object )" } "."
18 $nl
19 "Since the reader and writer are generic, words can be written which do not depend on the specific class of tuple passed in, but instead work on any tuple that defines slots with certain names."
20 $nl
21 "In most cases, using the setter is preferred over the writer because the stack effect is better suited to the common case where the tuple is needed again, and where the new slot value was just computed and so is at the top of the stack. For example, consider the case where you want to create a tuple and fill in the slots with literals. The following version uses setters:"
22 { $code
23     "<email>"
24     "    \"Happy birthday\" >>subject"
25     "    { \"bob@bigcorp.com\" } >>to"
26     "    \"alice@bigcorp.com\" >>from"
27     "send-email"
28 }
29 "The following uses writers, and requires some stack shuffling:"
30 { $code
31     "<email>"
32     "    \"Happy birthday\" over (>>subject)"
33     "    { \"bob@bigcorp.com\" } over (>>to)"
34     "    \"alice@bigcorp.com\" over (>>from)"
35     "send-email"
36 }
37 "Even if some of the slot values come from the stack underneath the tuple being constructed, setters win:"
38 { $code
39     "<email>"
40     "    swap >>subject"
41     "    swap >>to"
42     "    \"alice@bigcorp.com\" >>from"
43     "send-email"
44 }
45 "This is because " { $link swap } " is easier to understand than " { $link tuck } ":"
46 { $code
47     "<email>"
48     "    tuck (>>subject)"
49     "    tuck (>>to)"
50     "    \"alice@bigcorp.com\" over (>>from)"
51     "send-email"
52 }
53 "The changer word abstracts a common pattern where a slot value is read then stored again; so the following is not idiomatic code:"
54 { $code
55     "find-manager"
56     "    salary>> 0.75 * >>salary"
57 }
58 "The following version is preferred:"
59 { $code
60     "find-manager"
61     "    [ 0.75 * ] change-salary"
62 }
63 { $see-also "slots" "mirrors" } ;
64
65 ARTICLE: "slot-initial-values" "Initial values of slots"
66 "An initial value for a slot can be specified with the " { $link initial: } " slot declaration attribute. For certain classes, the initial value is optional; in these cases, it does not need to be specified. For others, it is required. Initial values can be used independently of class declaration, but if specified, the value must satisfy the class predicate."
67 $nl
68 "The following classes have default initial values:"
69 { $table
70     { { { $link f } } { $link f } }
71     { { { $link fixnum } } { $snippet "0" } }
72     { { { $link float } } { $snippet "0.0" } }
73     { { { $link string } } { $snippet "\"\"" } }
74     { { { $link byte-array } } { $snippet "B{ }" } }
75     { { { $link simple-alien } } { $snippet "BAD-ALIEN" } }
76 }
77 "All other classes are handled with one of two cases:"
78 { $list
79     { "If the class is a union or mixin class which " { $emphasis "contains" } " one of the above known classes, then the initial value of the class is that of the known class, with preference given to classes earlier in the list. For example, if the slot is declared " { $link object } " (this is the default), the initial value is " { $link f } ". Similarly for " { $link sequence } " and " { $link assoc } "." }
80     { "If the class is a tuple class, the initial value of the slot is a new, shared instance of the class created with " { $link new } "." }
81     { "Otherwise, a " { $link no-initial-value } " error is thrown. In this case, an initial value must be specified explicitly using " { $link initial: } "." }
82 }
83 "A word can be used to check if a class has an initial value or not:"
84 { $subsection initial-value } ;
85
86 ARTICLE: "slots" "Slots"
87 "A " { $emphasis "slot" } " is a component of an object which can store a value."
88 $nl
89 { $link "tuples" } " are composed entirely of slots, and instances of " { $link "builtin-classes" } " consist of slots together with intrinsic data."
90 "The " { $vocab-link "slots" } " vocabulary contains words for introspecting the slots of an object."
91 $nl
92 "The " { $snippet "\"slots\"" } " word property of built-in and tuple classes holds an array of " { $emphasis "slot specifiers" } " describing the slot layout of each instance."
93 { $subsection slot-spec }
94 "The four words associated with a slot can be looked up in the " { $vocab-link "accessors" } " vocabulary:"
95 { $subsection reader-word }
96 { $subsection writer-word }
97 { $subsection setter-word }
98 { $subsection changer-word }
99 "Looking up a slot by name:"
100 { $subsection slot-named }
101 "Defining slots dynamically:"
102 { $subsection define-reader }
103 { $subsection define-writer }
104 { $subsection define-setter }
105 { $subsection define-changer }
106 { $subsection define-slot-methods }
107 { $subsection define-accessors }
108 { $see-also "accessors" "mirrors" } ;
109
110 ABOUT: "slots"
111
112 HELP: slot-spec
113 { $class-description "A slot specification. The " { $snippet "\"slots\"" } " word property of " { $link builtin-class } " and " { $link tuple-class } " instances holds sequences of slot specifications."
114 $nl
115 "The slots of a slot specification are:"
116 { $list
117     { { $snippet "name" } " - a " { $link string } " identifying the slot." }
118     { { $snippet "offset" } " - an " { $link integer } " offset specifying where the slot value is stored inside instances of the relevant class. This is an implementation detail." }
119     { { $snippet "class" } " - a " { $link class } " declaring the set of possible values for the slot." }
120     { { $snippet "initial" } " - an initial value for the slot." }
121     { { $snippet "read-only" } " - a boolean indicating whether the slot is read only or not. Read only slots do not have a writer method associated with them." }
122 } } ;
123
124 HELP: define-typecheck
125 { $values { "class" class } { "generic" "a generic word" } { "quot" quotation } { "props" "an assoc of word properties" } }
126 { $description
127     "Defines a generic word with the " { $link standard-combination } " using dispatch position 0, and having one method on " { $snippet "class" } "."
128     $nl
129     "This creates a definition analogous to the following code:"
130     { $code
131         "GENERIC: generic"
132         "M: class generic quot ;"
133     }
134     "It checks if the top of the stack is an instance of " { $snippet "class" } ", and if so, executes the quotation."
135 }
136 { $notes "This word is used internally to wrap unsafe low-level code in a type-checking stub." } ;
137
138 HELP: define-reader
139 { $values { "class" class } { "slot-spec" slot-spec } }
140 { $description "Defines a reader word to read a slot specified by " { $snippet "slot-spec" } "." }
141 $low-level-note ;
142
143 HELP: define-writer
144 { $values { "class" class } { "slot-spec" slot-spec } }
145 { $description "Defines a generic word " { $snippet "writer" } " to write a new value to a slot specified by " { $snippet "slot-spec" } "." }
146 $low-level-note ;
147
148 HELP: define-slot-methods
149 { $values { "class" class } { "slot-spec" slot-spec } }
150 { $description "Defines a reader, writer, setter and changer for a slot specified by " { $snippet "slot-spec" } "." }
151 $low-level-note ;
152
153 HELP: define-accessors
154 { $values { "class" class } { "specs" "a sequence of " { $link slot-spec } " instances" } }
155 { $description "Defines slot methods." }
156 $low-level-note ;
157
158 HELP: slot ( obj m -- value )
159 { $values { "obj" object } { "m" "a non-negative fixnum" } { "value" object } }
160 { $description "Reads the object stored at the " { $snippet "n" } "th slot of " { $snippet "obj" } "." }
161 { $warning "This word is in the " { $vocab-link "slots.private" } " vocabulary because it does not perform type or bounds checks, and slot numbers are implementation detail." } ;
162
163 HELP: set-slot ( value obj n -- )
164 { $values { "value" object } { "obj" object } { "n" "a non-negative fixnum" } }
165 { $description "Writes " { $snippet "value" } " to the " { $snippet "n" } "th slot of " { $snippet "obj" } "." }
166 { $warning "This word is in the " { $vocab-link "slots.private" } " vocabulary because it does not perform type or bounds checks, and slot numbers are implementation detail." } ;
167
168 HELP: slot-named
169 { $values { "name" string } { "specs" "a sequence of " { $link slot-spec } " instances" } { "spec/f" "a " { $link slot-spec } " or " { $link f } } }
170 { $description "Outputs the " { $link slot-spec } " with the given name." } ;