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
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."
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."
13 "In addition, two utility words are defined for each writable slot."
15 "The " { $emphasis "setter" } " is named " { $snippet ">>" { $emphasis "slot" } } " and stores a value into a slot. It has stack effect " { $snippet "( object value -- object )" } "."
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 )" } "."
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."
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:"
24 " \"Happy birthday\" >>subject"
25 " { \"bob@bigcorp.com\" } >>to"
26 " \"alice@bigcorp.com\" >>from"
29 "The following uses writers, and requires some stack shuffling:"
32 " \"Happy birthday\" over (>>subject)"
33 " { \"bob@bigcorp.com\" } over (>>to)"
34 " \"alice@bigcorp.com\" over (>>from)"
37 "Even if some of the slot values come from the stack underneath the tuple being constructed, setters win:"
42 " \"alice@bigcorp.com\" >>from"
45 "This is because " { $link swap } " is easier to understand than " { $link tuck } ":"
50 " \"alice@bigcorp.com\" over (>>from)"
53 "The changer word abstracts a common pattern where a slot value is read then stored again; so the following is not idiomatic code:"
56 " salary>> 0.75 * >>salary"
58 "The following version is preferred:"
61 " [ 0.75 * ] change-salary"
63 { $see-also "slots" "mirrors" } ;
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."
68 "The following classes have default initial values:"
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" } }
77 "All other classes are handled with one of two cases:"
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: } "." }
83 "A word can be used to check if a class has an initial value or not:"
84 { $subsection initial-value } ;
86 ARTICLE: "slots" "Slots"
87 "A " { $emphasis "slot" } " is a component of an object which can store a value."
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."
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" } ;
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."
115 "The slots of a slot specification are:"
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." }
124 HELP: define-typecheck
125 { $values { "class" class } { "generic" "a generic word" } { "quot" quotation } { "props" "an assoc of word properties" } }
127 "Defines a generic word with the " { $link standard-combination } " using dispatch position 0, and having one method on " { $snippet "class" } "."
129 "This creates a definition analogous to the following code:"
132 "M: class generic quot ;"
134 "It checks if the top of the stack is an instance of " { $snippet "class" } ", and if so, executes the quotation."
136 { $notes "This word is used internally to wrap unsafe low-level code in a type-checking stub." } ;
139 { $values { "class" class } { "slot-spec" slot-spec } }
140 { $description "Defines a reader word to read a slot specified by " { $snippet "slot-spec" } "." }
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" } "." }
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" } "." }
153 HELP: define-accessors
154 { $values { "class" class } { "specs" "a sequence of " { $link slot-spec } " instances" } }
155 { $description "Defines slot methods." }
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." } ;
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." } ;
169 { $values { "name" string } { "specs" "a sequence of " { $link slot-spec } " instances" } { "spec/f" { $maybe slot-spec } } }
170 { $description "Outputs the " { $link slot-spec } " with the given name." } ;