]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic-docs.factor
Update actions, because Node.js 16 actions are deprecated, to Node.js 20
[factor.git] / core / generic / generic-docs.factor
1 USING: classes classes.algebra combinators effects generic.hook
2 generic.math generic.single generic.standard help.markup
3 help.syntax math words ;
4 IN: generic
5
6 ARTICLE: "method-order" "Method precedence"
7 "Conceptually, method dispatch is implemented by testing the object against the predicate word for every class, in linear order (" { $link "class-linearization" } ")."
8 $nl
9 "Here is an example:"
10 { $code
11     "GENERIC: explain ( object -- )"
12     "M: object explain drop \"an object\" print ;"
13     "M: generic explain drop \"a generic word\" print ;"
14     "M: class explain drop \"a class word\" print ;"
15 }
16 "The linear order is the following, from least-specific to most-specific:"
17 { $code "{ object generic class }" }
18 "Neither " { $link class } " nor " { $link generic } " are subclasses of each other, and their intersection is non-empty. Calling " { $snippet "explain" } " with a word on the stack that is both a class and a generic word will print " { $snippet "a class word" } " because " { $link class } " is more specific than " { $link generic } " in the class linearization order. (One example of a word which is both a class and a generic word is the class of classes, " { $link class } ", which is also a word to get the class of an object.)"
19 $nl
20 "The " { $link order } " word can be useful to clarify method dispatch order:"
21 { $subsections order } ;
22
23 ARTICLE: "generic-introspection" "Generic word introspection"
24 "In most cases, generic words and methods are defined at parse time with " { $link POSTPONE: GENERIC: } " (or some other parsing word) and " { $link POSTPONE: M: } "."
25 $nl
26 "Sometimes, generic words need to be inspected or defined at run time; words for performing these tasks are found in the " { $vocab-link "generic" } " vocabulary."
27 $nl
28 "The set of generic words is a class which implements the " { $link "definition-protocol" } ":"
29 { $subsections
30     generic
31     generic?
32 }
33 "New generic words can be defined:"
34 { $subsections
35     define-generic
36     define-simple-generic
37 }
38 "Methods can be added to existing generic words:"
39 { $subsections create-method }
40 "Method definitions can be looked up:"
41 { $subsections lookup-method ?lookup-method }
42 "Finding the most specific method for an object:"
43 { $subsections effective-method }
44 "A generic word contains methods; the list of methods specializing on a class can also be obtained:"
45 { $subsections implementors }
46 "Low-level word which rebuilds the generic word after methods are added or removed, or the method combination is changed:"
47 { $subsections make-generic }
48 "Low-level method constructor:"
49 { $subsections <method> }
50 "Methods may be pushed on the stack with a literal syntax:"
51 { $subsections POSTPONE: M\ }
52 { $see-also "see" } ;
53
54 ARTICLE: "method-combination" "Custom method combination"
55 "Abstractly, a generic word can be thought of as a big chain of type conditional tests applied to the top of the stack, with methods as the bodies of each test. The " { $emphasis "method combination" } " is this control flow glue between the set of methods, and several aspects of it can be customized:"
56 { $list
57     "which stack item(s) the generic word dispatches upon,"
58     "which methods out of the set of applicable methods are called"
59 }
60 "A table of built-in method combination defining words, and the method combinations themselves:"
61 { $table
62     { { $link POSTPONE: GENERIC: } { $link standard-combination } }
63     { { $link POSTPONE: GENERIC#: } { $link standard-combination } }
64     { { $link POSTPONE: HOOK: } { $link hook-combination } }
65     { { $link POSTPONE: MATH: } { $link math-combination } }
66 }
67 "Developing a custom method combination requires that a parsing word calling " { $link define-generic } " be defined; additionally, it is a good idea to implement the " { $link "definition-protocol" } " on the class of words having this method combination, to properly support developer tools."
68 $nl
69 "The combination quotation passed to " { $link define-generic } " has stack effect " { $snippet "( word -- quot )" } ". It's job is to call various introspection words, including at least obtaining the set of methods defined on the generic word, then combining these methods in some way to produce a quotation."
70 { $see-also "generic-introspection" } ;
71
72 ARTICLE: "call-next-method" "Calling less-specific methods"
73 "If a generic word is called with an object and multiple methods specialize on classes that this object is an instance of, usually the most specific method is called (" { $link "method-order" } ")."
74 $nl
75 "Less-specific methods can be called directly:"
76 { $subsections POSTPONE: call-next-method }
77 "A lower-level word which the above expands into:"
78 { $subsections (call-next-method) }
79 "To look up the next applicable method reflectively:"
80 { $subsections next-method }
81 "Errors thrown by improper calls to " { $link POSTPONE: call-next-method } ":"
82 { $subsections
83     inconsistent-next-method
84     no-next-method
85 } ;
86
87 ARTICLE: "generic" "Generic words and methods"
88 "A " { $emphasis "generic word" } " is composed of zero or more " { $emphasis "methods" } " together with a " { $emphasis "method combination" } ". A method " { $emphasis "specializes" } " on a class; when a generic word is executed, the method combination chooses the most appropriate method and calls its definition."
89 $nl
90 "A generic word behaves roughly like a long series of class predicate conditionals in a " { $link cond } " form, however methods can be defined in independent source files, reducing coupling and increasing extensibility. The method combination determines which object the generic word will " { $emphasis "dispatch" } " on; this could be the top of the stack, or some other value."
91 $nl
92 "Generic words which dispatch on the object at the top of the stack:"
93 { $subsections POSTPONE: GENERIC: }
94 "A method combination which dispatches on a specified stack position:"
95 { $subsections POSTPONE: GENERIC#: }
96 "A method combination which dispatches on the value of a variable at the time the generic word is called:"
97 { $subsections POSTPONE: HOOK: }
98 "A method combination which dispatches on a pair of stack values, which must be numbers, and upgrades both to the same type of number:"
99 { $subsections POSTPONE: MATH: }
100 "Method definition:"
101 { $subsections POSTPONE: M: }
102 "Generic words must declare their stack effect in order to compile. See " { $link "effects" } "."
103 { $subsections
104     "method-order"
105     "call-next-method"
106     "method-combination"
107     "generic-introspection"
108 }
109 "Generic words specialize behavior based on the class of an object; sometimes behavior needs to be specialized on the object's " { $emphasis "structure" } "; this is known as " { $emphasis "pattern matching" } " and is implemented in the " { $vocab-link "match" } " vocabulary." ;
110
111 ABOUT: "generic"
112
113 HELP: generic
114 { $class-description "The class of generic words, documented in " { $link "generic" } "." } ;
115
116 { generic define-generic define-simple-generic POSTPONE: GENERIC: POSTPONE: GENERIC#: POSTPONE: MATH: POSTPONE: HOOK: } related-words
117
118 HELP: make-generic
119 { $values { "word" generic } }
120 { $description "Regenerates the definition of a generic word by applying the method combination to the set of defined methods." }
121 $low-level-note ;
122
123 HELP: define-generic
124 { $values { "word" word } { "combination" "a method combination" } { "effect" effect } }
125 { $description "Defines a generic word. A method combination is an object which responds to the " { $link perform-combination } " generic word." }
126 { $contract "The method combination quotation is called each time the generic word has to be updated (for example, when a method is added), and thus must be side-effect free." } ;
127
128 HELP: M\
129 { $syntax "M\\ class generic" }
130 { $description "Pushes a method on the stack." }
131 { $examples { $code "M\\ fixnum + see" } { $code "USING: ui.gadgets.editors ui.render ;" "M\\ editor draw-gadget* edit" } } ;
132
133 HELP: method
134 { $class-description "The class of method bodies, which are words with special word properties set." } ;
135
136 HELP: lookup-method
137 { $values { "class" class } { "generic" generic } { "method" method } }
138 { $description "Looks up a method definition." }
139 { $errors "Throws an error if the method does not exist." } ;
140
141 HELP: ?lookup-method
142 { $values { "class" class } { "generic" generic } { "method/f" { $maybe method } } }
143 { $description "Looks up a method definition." } ;
144
145 { lookup-method ?lookup-method create-method POSTPONE: M: } related-words
146
147 HELP: <method>
148 { $values { "class" class } { "generic" generic } { "method" "a new method definition" } }
149 { $description "Creates a new method." } ;
150
151 HELP: order
152 { $values { "generic" generic } { "seq" { $sequence class } } }
153 { $description "Outputs a sequence of classes for which methods have been defined on this generic word. The sequence is sorted in method dispatch order." } ;
154
155 HELP: check-method
156 { $values { "class" class } { "generic" generic } }
157 { $description "Asserts that " { $snippet "class" } " is a class word and " { $snippet "generic" } " is a generic word, throwing a " { $link check-method } " error if the assertion fails." }
158 { $error-description "Thrown if " { $link POSTPONE: M: } " or " { $link create-method } " is given an invalid class or generic word." } ;
159
160 HELP: with-methods
161 { $values { "class" class } { "generic" generic } { "quot" { $quotation ( methods -- ) } } }
162 { $description "Applies a quotation to the generic word's methods hashtable, and regenerates the generic word's definition when the quotation returns." }
163 $low-level-note ;
164
165 HELP: create-method
166 { $values { "class" class } { "generic" generic } { "method" method } }
167 { $description "Creates a method or returns an existing one. This is the runtime equivalent of " { $link POSTPONE: M: } "." }
168 { $notes "To define a method, pass the output value to " { $link define } "." } ;
169
170 { sort-classes order } related-words
171
172 HELP: (call-next-method)
173 { $values { "method" method } }
174 { $description "Low-level word implementing " { $link POSTPONE: call-next-method } "." }
175 { $notes
176     "The " { $link POSTPONE: call-next-method } " word parses into this word. The following are equivalent:"
177     { $code
178         "M: class generic call-next-method ;"
179         "M: class generic M\\ class generic (call-next-method) ;"
180     }
181 } ;
182
183 HELP: no-next-method
184 { $error-description "Thrown by " { $link POSTPONE: call-next-method } " if the current method is already the least specific method." }
185 { $examples
186     "The following code throws this error:"
187     { $code
188         "GENERIC: error-test ( object -- )"
189         ""
190         "M: number error-test 3 + call-next-method ;"
191         ""
192         "M: integer error-test recip call-next-method ;"
193         ""
194         "123 error-test"
195     }
196     "This results in the method on " { $link integer } " being called, which then calls the method on " { $link number } ". The latter then calls " { $link POSTPONE: call-next-method } ", however there is no method less specific than the method on " { $link number } " and so an error is thrown."
197 } ;