]> gitweb.factorcode.org Git - factor.git/blob - core/classes/classes-docs.factor
factor: superclass -> superclass-of, superclasses -> superclasses-of
[factor.git] / core / classes / classes-docs.factor
1 USING: classes.private help.markup help.syntax kernel quotations
2 sequences words ;
3 IN: classes
4
5 ARTICLE: "class-predicates" "Class predicate words"
6 "With a handful of exceptions, each class has a membership predicate word, named " { $snippet { $emphasis "class" } "?" } ". A quotation calling this predicate is stored in the " { $snippet "\"predicate\"" } " word property."
7 $nl
8 "When it comes to predicates, the exceptional classes are:"
9 { $table
10     { "Class" "Predicate" "Explanation" }
11     { { $link f } { $snippet "[ not ]" } { "The conventional name for a word which outputs true when given false is " { $link not } "; " { $snippet "f?" } " would be confusing." } }
12     { { $link object } { $snippet "[ drop t ]" } { "All objects are instances of " { $link object } } }
13     { { $link null } { $snippet "[ drop f ]" } { "No object is an instance of " { $link null } } }
14 }
15 "The set of class predicate words is a class:"
16 { $subsections
17     predicate
18     predicate?
19 }
20 "A predicate word holds a reference to the class it is predicating over in the " { $snippet "\"predicating\"" } " word property." $nl
21 "Implementation of class reloading:"
22 { $subsections reset-class forget-class forget-methods } ;
23
24 ARTICLE: "classes" "Classes"
25 "Conceptually, a " { $snippet "class" } " is a set of objects whose members can be identified with a predicate, and on which generic words can specialize methods. Classes are organized into a general partial order, and an object may be an instance of more than one class."
26 $nl
27 "At the implementation level, a class is a word with certain word properties set."
28 $nl
29 "Words for working with classes are found in the " { $vocab-link "classes" } " vocabulary."
30 $nl
31 "Classes themselves form a class:"
32 { $subsections class? }
33 "You can ask an object for its class:"
34 { $subsections class-of }
35 "Testing if an object is an instance of a class:"
36 { $subsections instance? }
37 "You can ask a class for its superclass:"
38 { $subsections
39     superclass-of
40     superclasses-of
41     subclass-of?
42 }
43 "Class predicates can be used to test instances directly:"
44 { $subsections "class-predicates" }
45 "There is a universal class which all objects are an instance of, and an empty class with no instances:"
46 { $subsections
47     object
48     null
49 }
50 "Obtaining a list of all defined classes:"
51 { $subsections classes }
52 "There are several sorts of classes:"
53 { $subsections
54     "builtin-classes"
55     "unions"
56     "intersections"
57     "mixins"
58     "predicates"
59     "singletons"
60 }
61 { $link "tuples" } " are documented in their own section."
62 $nl
63 "Classes can be inspected and operated upon:"
64 { $subsections
65     "class-operations"
66     "class-linearization"
67 }
68 { $see-also "class-index" } ;
69
70 ABOUT: "classes"
71
72 HELP: class
73 { $class-description "The class of all class words." } ;
74
75 HELP: class-of
76 { $values { "object" object } { "class" class } }
77 { $description "Outputs an object's canonical class. While an object may be an instance of more than one class, the canonical class is either its built-in class, or if the object is a tuple, its tuple class." }
78 { $examples { $example "USING: classes prettyprint ;" "1.0 class-of ." "float" } { $example "USING: classes prettyprint ;" "IN: scratchpad" "TUPLE: point x y z ;\nT{ point f 1 2 3 } class-of ." "point" } } ;
79
80 HELP: classes
81 { $values { "seq" "a sequence of class words" } }
82 { $description "Finds all class words in the dictionary." } ;
83
84 HELP: update-map
85 { $var-description "Assoc mapping each class to a set of classes defined in terms of this class. The " { $link define-class } " word uses this information to update generic words when classes are redefined." } ;
86
87 HELP: predicate-def
88 { $values { "obj" "a type object" } { "quot" quotation } }
89 { $description "Outputs a quotation that can be used to check if objects are an instance of the given type." }
90 { $examples
91   { $example
92     "USING: classes math prettyprint ;"
93     "fixnum predicate-def ."
94     "[ fixnum? ]"
95   }
96 } ;
97
98 HELP: predicate-word
99 { $values { "word" word } { "predicate" "a predicate word" } }
100 { $description "Suffixes the word's name with \"?\" and creates a word with that name in the same vocabulary as the word itself." } ;
101
102 HELP: define-predicate
103 { $values { "class" class } { "quot" quotation } }
104 { $description "Defines a predicate word for a class." }
105 $low-level-note ;
106
107 HELP: superclass-of
108 { $values { "class" class } { "super" class } }
109 { $description "Outputs the superclass of a class. All instances of this class are also instances of the superclass." }
110 { $examples
111     { $example "USING: classes prettyprint ;"
112                "t superclass-of ."
113                "word"
114     }
115 } ;
116
117 HELP: superclasses-of
118 { $values
119      { "class" class }
120      { "supers" sequence } }
121 { $description "Outputs a sequence of superclasses of a class along with the class itself." }
122 { $examples
123     { $example "USING: classes prettyprint ;"
124                "t superclasses-of ."
125                "{ word t }"
126     }
127 } ;
128
129 HELP: subclass-of?
130 { $values
131     { "class" class }
132     { "superclass" class }
133     { "?" boolean }
134 }
135 { $description "Outputs a boolean value indicating whether " { $snippet "class" } " is at any level a subclass of " { $snippet "superclass" } "." }
136 { $examples
137     { $example "USING: classes classes.tuple prettyprint words ;"
138                "tuple-class \\ class subclass-of? ."
139                "t"
140     }
141 } ;
142
143 { superclass-of superclasses-of subclass-of? } related-words
144
145 HELP: class-members
146 { $values { "class" class } { "seq" "a sequence of union members, or " { $link f } } }
147 { $description "If " { $snippet "class" } " is a union class, outputs a sequence of its member classes, otherwise outputs " { $link f } "." } ;
148
149 HELP: class-participants
150 { $values { "class" class } { "seq" "a sequence of intersection participants, or " { $link f } } }
151 { $description "If " { $snippet "class" } " is an intersection class, outputs a sequence of its participant classes, otherwise outputs " { $link f } "." } ;
152
153 HELP: define-class
154 { $values { "word" word } { "superclass" class } { "members" "a sequence of class words" } { "participants" "a sequence of class words" } { "metaclass" class } }
155 { $description "Sets a property indicating this word is a class word, thus making it an instance of " { $link class } ", and registers it with " { $link update-map } "." }
156 $low-level-note ;
157
158 HELP: implementors
159 { $values { "class/classes" "a class or a sequence of classes" } { "seq" "a sequence of generic words" } }
160 { $description "Finds all generic words in the dictionary implementing methods for the given set of classes." } ;
161
162 HELP: instance?
163 { $values
164      { "object" object } { "class" class }
165      { "?" boolean } }
166 { $description "Tests whether the input object is a member of the class." } ;
167
168 HELP: reset-class
169 { $values { "class" class } }
170 { $description "Forgets all of words that the class defines, but not words that are defined on the class. For instance, on a tuple class, this word should reset all of the tuple accessors but not things like " { $link nth } " that may be defined on the class elsewhere." } ;
171
172 HELP: forget-class
173 { $values { "class" class } }
174 { $description "Removes a class by forgetting all of the methods defined on that class and all of the methods generated when that class was defined. Also resets any caches that may contain that class." } ;
175
176 HELP: forget-methods
177 { $values { "class" class } }
178 { $description "Forgets all methods defined on a class. In contrast to " { $link reset-class } ", this not only forgets accessors but also any methods at all on the class." } ;