Commit | Line | Data |
---|---|---|
d99832a2 NJ |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
3 | @c Copyright (C) 2008, 2009 | |
4 | @c Free Software Foundation, Inc. | |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
a0e07ba4 NJ |
7 | @macro goops |
8 | GOOPS | |
9 | @end macro | |
10 | ||
11 | @macro guile | |
12 | Guile | |
13 | @end macro | |
14 | ||
eb12b401 NJ |
15 | @node GOOPS |
16 | @chapter GOOPS | |
a0e07ba4 | 17 | |
a0e07ba4 NJ |
18 | @goops{} is the object oriented extension to @guile{}. Its |
19 | implementation is derived from @w{STk-3.99.3} by Erick Gallesio and | |
56664c08 | 20 | version 1.3 of Gregor Kiczales' @cite{Tiny-Clos}. It is very close in |
b997e7f5 NJ |
21 | spirit to CLOS, the Common Lisp Object System, but is adapted for the |
22 | Scheme language. | |
23 | ||
24 | @goops{} is a full object oriented system, with classes, objects, | |
25 | multiple inheritance, and generic functions with multi-method | |
26 | dispatch. Furthermore its implementation relies on a meta object | |
27 | protocol --- which means that @goops{}'s core operations are themselves | |
28 | defined as methods on relevant classes, and can be customised by | |
29 | overriding or redefining those methods. | |
a0e07ba4 | 30 | |
769be03f | 31 | @menu |
b65ff721 | 32 | * Copyright Notice:: |
9e7ec8d1 | 33 | * Quick Start:: |
e946b0b9 | 34 | * Tutorial:: |
c34d74ff NJ |
35 | * Defining New Classes:: |
36 | * Creating Instances:: | |
37 | * Accessing Slots:: | |
38 | * Creating Generic Functions:: | |
39 | * Adding Methods to Generic Functions:: | |
40 | * Invoking Generic Functions:: | |
41 | * Redefining a Class:: | |
42 | * Changing the Class of an Instance:: | |
43 | * Introspection:: | |
44 | * Miscellaneous Functions:: | |
a9bf12c2 | 45 | * The Metaobject Protocol:: |
769be03f NJ |
46 | @end menu |
47 | ||
b65ff721 NJ |
48 | @node Copyright Notice |
49 | @section Copyright Notice | |
50 | ||
51 | The material in this chapter is partly derived from the STk Reference | |
52 | Manual written by Erick Gallesio, whose copyright notice is as follows. | |
53 | ||
54 | Copyright © 1993-1999 Erick Gallesio - I3S-CNRS/ESSI <eg@@unice.fr> | |
55 | Permission to use, copy, modify, distribute,and license this | |
56 | software and its documentation for any purpose is hereby granted, | |
57 | provided that existing copyright notices are retained in all | |
58 | copies and that this notice is included verbatim in any | |
59 | distributions. No written agreement, license, or royalty fee is | |
60 | required for any of the authorized uses. | |
61 | This software is provided ``AS IS'' without express or implied | |
62 | warranty. | |
63 | ||
64 | The material has been adapted for use in Guile, with the author's | |
65 | permission. | |
66 | ||
67 | ||
9e7ec8d1 NJ |
68 | @node Quick Start |
69 | @section Quick Start | |
70 | ||
e946b0b9 | 71 | To start using GOOPS, load the @code{(oop goops)} module: |
a0e07ba4 | 72 | |
aba0dff5 | 73 | @lisp |
a0e07ba4 | 74 | (use-modules (oop goops)) |
aba0dff5 | 75 | @end lisp |
a0e07ba4 | 76 | |
a0e07ba4 NJ |
77 | We're now ready to try some basic GOOPS functionality. |
78 | ||
e946b0b9 NJ |
79 | @menu |
80 | * Methods:: | |
5268eca6 | 81 | * Built-in classes:: |
de69bd28 | 82 | * User-defined classes:: |
e946b0b9 NJ |
83 | @end menu |
84 | ||
de69bd28 | 85 | |
eb12b401 | 86 | @node Methods |
a0e07ba4 NJ |
87 | @subsection Methods |
88 | ||
9e7ec8d1 | 89 | A GOOPS method is like a Scheme procedure except that it is |
de69bd28 | 90 | specialized for a particular set of argument classes. |
9e7ec8d1 NJ |
91 | |
92 | @lisp | |
a0e07ba4 NJ |
93 | (define-method (+ (x <string>) (y <string>)) |
94 | (string-append x y)) | |
95 | ||
f618f436 | 96 | (+ "abc" "de") @result{} "abcde" |
9e7ec8d1 NJ |
97 | @end lisp |
98 | ||
99 | If @code{+} is used with arguments that do not match the method's | |
de69bd28 | 100 | classes, Guile falls back to using the normal Scheme @code{+} procedure. |
9e7ec8d1 NJ |
101 | |
102 | @lisp | |
f618f436 | 103 | (+ 1 2) @result{} 3 |
9e7ec8d1 NJ |
104 | @end lisp |
105 | ||
a0e07ba4 | 106 | |
5268eca6 NJ |
107 | @node Built-in classes |
108 | @subsection Built-in classes | |
109 | ||
110 | There are built-in classes like @code{<string>}, @code{<list>} and | |
111 | @code{<number>} corresponding to all the Guile Scheme types. You can | |
112 | use the @code{is-a?} predicate to ask whether any given value belongs to | |
113 | a given class, or @code{class-of} to discover the class of a given | |
114 | value. | |
115 | ||
116 | @lisp | |
117 | (is-a? 2.3 <number>) @result{} #t | |
118 | (is-a? 2.3 <real>) @result{} #t | |
119 | (is-a? 2.3 <string>) @result{} #f | |
120 | (is-a? '("a" "b") <string>) @result{} #f | |
121 | (is-a? '("a" "b") <list>) @result{} #t | |
122 | (is-a? (car '("a" "b")) <string>) @result{} #t | |
123 | (is-a? <string> <class>) @result{} #t | |
124 | (is-a? <class> <string>) @result{} #f | |
125 | ||
126 | (class-of 2.3) @result{} #<<class> <real> 908c708> | |
127 | (class-of #(1 2 3)) @result{} #<<class> <vector> 908cd20> | |
128 | (class-of <string>) @result{} #<<class> <class> 8bd3e10> | |
129 | (class-of <class>) @result{} #<<class> <class> 8bd3e10> | |
130 | @end lisp | |
131 | ||
132 | ||
de69bd28 NJ |
133 | @node User-defined classes |
134 | @subsection User-defined classes | |
a0e07ba4 | 135 | |
5a6c9e75 NJ |
136 | You can, of course, also define new classes. The GOOPS term for |
137 | ``fields'' or ``member variables'' is @dfn{slots}, and the main thing | |
138 | you have to specify, when defining a new class, is what its slots will | |
139 | be, and how they will be initialised and accessed. | |
140 | ||
9e7ec8d1 | 141 | @lisp |
a0e07ba4 NJ |
142 | (define-class <2D-vector> () |
143 | (x #:init-value 0 #:accessor x-component #:init-keyword #:x) | |
144 | (y #:init-value 0 #:accessor y-component #:init-keyword #:y)) | |
5a6c9e75 NJ |
145 | @end lisp |
146 | ||
147 | Methods are not (formally) part of a specific class's definition, | |
148 | because a single method can be associated with several classes. If | |
149 | you've studied object orientation in non-Lispy languages, you may | |
150 | remember discussions such as whether a method to stretch a graphical | |
151 | image around a surface should be a method of the image class, with a | |
152 | surface as a parameter, or a method of the surface class, with an image | |
153 | as a parameter. In the generic function approach that GOOPS provides, | |
154 | this question does not arise. | |
a0e07ba4 | 155 | |
5a6c9e75 NJ |
156 | Here we customise @code{write} for the new class, so that |
157 | @code{<2D-vector>} objects will be nicely printed: | |
158 | ||
159 | @lisp | |
a0e07ba4 NJ |
160 | (use-modules (ice-9 format)) |
161 | ||
162 | (define-method (write (obj <2D-vector>) port) | |
3d8e6eb8 | 163 | (format port "<~S, ~S>" (x-component obj) (y-component obj))) |
5a6c9e75 | 164 | @end lisp |
a0e07ba4 | 165 | |
5a6c9e75 NJ |
166 | To make an @dfn{instance} or @dfn{object} of your new class, use |
167 | @code{make} together with the class and any initialisation arguments: | |
168 | ||
169 | @lisp | |
a0e07ba4 NJ |
170 | (define v (make <2D-vector> #:x 3 #:y 4)) |
171 | ||
f618f436 | 172 | v @result{} <3, 4> |
5268eca6 NJ |
173 | (is-a? v <2D-vector>) @result{} #t |
174 | (class-of v) @result{} #<<class> <2D-vector> 40241ac0> | |
5a6c9e75 | 175 | @end lisp |
a0e07ba4 | 176 | |
5a6c9e75 NJ |
177 | Here is another method that is specialised for @code{<2D-vector>} |
178 | objects, to add two of them together: | |
179 | ||
180 | @lisp | |
a0e07ba4 NJ |
181 | (define-method (+ (x <2D-vector>) (y <2D-vector>)) |
182 | (make <2D-vector> | |
183 | #:x (+ (x-component x) (x-component y)) | |
184 | #:y (+ (y-component x) (y-component y)))) | |
185 | ||
f618f436 | 186 | (+ v v) @result{} <6, 8> |
9e7ec8d1 | 187 | @end lisp |
a0e07ba4 | 188 | |
a0e07ba4 | 189 | |
e946b0b9 NJ |
190 | @node Tutorial |
191 | @section Tutorial | |
192 | @include goops-tutorial.texi | |
193 | ||
8445eb1d | 194 | |
a9bf12c2 NJ |
195 | @node Defining New Classes |
196 | @section Defining New Classes | |
a0e07ba4 | 197 | |
a9bf12c2 NJ |
198 | New classes are defined using the @code{define-class} syntax, with |
199 | arguments that specify the classes that the new class should inherit | |
8445eb1d | 200 | from, the direct slots of the new class, and any class options. |
a0e07ba4 | 201 | |
a9bf12c2 NJ |
202 | @deffn syntax define-class name (super @dots{}) slot-definition @dots{} . options |
203 | Define a class called @var{name} that inherits from @var{super}s, with | |
204 | direct slots defined by @var{slot-definition}s and class options | |
205 | @var{options}. The newly created class is bound to the variable name | |
206 | @var{name} in the current environment. | |
a0e07ba4 | 207 | |
a9bf12c2 NJ |
208 | Each @var{slot-definition} is either a symbol that names the slot or a |
209 | list, | |
a0e07ba4 | 210 | |
a9bf12c2 NJ |
211 | @example |
212 | (@var{slot-name-symbol} . @var{slot-options}) | |
213 | @end example | |
a0e07ba4 | 214 | |
a9bf12c2 NJ |
215 | where @var{slot-name-symbol} is a symbol and @var{slot-options} is a |
216 | list with an even number of elements. The even-numbered elements of | |
217 | @var{slot-options} (counting from zero) are slot option keywords; the | |
218 | odd-numbered elements are the corresponding values for those keywords. | |
a0e07ba4 | 219 | |
a9bf12c2 NJ |
220 | @var{options} is a similarly structured list containing class option |
221 | keywords and corresponding values. | |
222 | @end deffn | |
a0e07ba4 | 223 | |
a9bf12c2 NJ |
224 | Example 1. Define a class that combines two pre-existing classes by |
225 | inheritance but adds no new slots. | |
a0e07ba4 NJ |
226 | |
227 | @example | |
a9bf12c2 | 228 | (define-class <combined> (<tree> <bicycle>)) |
a0e07ba4 NJ |
229 | @end example |
230 | ||
a9bf12c2 | 231 | Example 2. Define a @code{regular-polygon} class with slots for side |
8445eb1d NJ |
232 | length and number of sides. The slots have default values and can be |
233 | accessed via the generic functions @code{side-length} and | |
234 | @code{num-sides}. | |
a0e07ba4 NJ |
235 | |
236 | @example | |
a9bf12c2 NJ |
237 | (define-class <regular-polygon> () |
238 | (sl #:init-value 1 #:accessor side-length) | |
239 | (ns #:init-value 5 #:accessor num-sides)) | |
a0e07ba4 NJ |
240 | @end example |
241 | ||
a9bf12c2 NJ |
242 | Example 3. Define a class whose behavior (and that of its instances) is |
243 | customized via an application-defined metaclass. | |
a0e07ba4 NJ |
244 | |
245 | @example | |
a9bf12c2 NJ |
246 | (define-class <tcpip-fsm> () |
247 | (s #:init-value #f #:accessor state) | |
248 | ... | |
249 | #:metaclass <finite-state-class>) | |
a0e07ba4 NJ |
250 | @end example |
251 | ||
8445eb1d NJ |
252 | The possible slot and class options are described in the following |
253 | subsections. | |
a0e07ba4 | 254 | |
8445eb1d NJ |
255 | @menu |
256 | * Slot Options:: | |
257 | * Class Options:: | |
258 | * Class Definition Internals:: | |
259 | * Customizing Class Definition:: | |
260 | * STKlos Compatibility:: | |
261 | @end menu | |
a0e07ba4 NJ |
262 | |
263 | @node Slot Options | |
c34d74ff | 264 | @subsection Slot Options |
a0e07ba4 | 265 | |
a0e07ba4 NJ |
266 | @deffn {slot option} #:init-value init-value |
267 | @deffnx {slot option} #:init-form init-form | |
268 | @deffnx {slot option} #:init-thunk init-thunk | |
269 | @deffnx {slot option} #:init-keyword init-keyword | |
270 | These options provide various ways to specify how to initialize the | |
271 | slot's value at instance creation time. @var{init-value} is a fixed | |
35369f45 KR |
272 | value (shared across all new instances of the class). |
273 | @var{init-thunk} is a procedure of no arguments that is called | |
a0e07ba4 NJ |
274 | when a new instance is created and should return the desired initial |
275 | slot value. @var{init-form} is an unevaluated expression that gets | |
276 | evaluated when a new instance is created and should return the desired | |
5695ccd4 NJ |
277 | initial slot value. @var{init-keyword} is a keyword that can be used |
278 | to pass an initial slot value to @code{make} when creating a new | |
279 | instance. | |
280 | ||
281 | Note that, since an @code{init-value} value is shared across all | |
282 | instances of a class, you should only use it when the initial value is | |
283 | an immutable value, like a constant. If you want to initialize a slot | |
284 | with a fresh, independently mutable value, you should use | |
285 | @code{init-thunk} or @code{init-form} instead. Consider the following | |
286 | example. | |
287 | ||
288 | @example | |
289 | (define-class <chbouib> () | |
290 | (hashtab #:init-value (make-hash-table))) | |
291 | @end example | |
292 | ||
293 | @noindent | |
294 | Here only one hash table is created and all instances of | |
295 | @code{<chbouib>} have their @code{hashtab} slot refer to it. In order | |
296 | to have each instance of @code{<chbouib>} refer to a new hash table, you | |
297 | should instead write: | |
298 | ||
299 | @example | |
300 | (define-class <chbouib> () | |
301 | (hashtab #:init-thunk make-hash-table)) | |
302 | @end example | |
303 | ||
304 | @noindent | |
305 | or: | |
306 | ||
307 | @example | |
308 | (define-class <chbouib> () | |
309 | (hashtab #:init-form (make-hash-table))) | |
310 | @end example | |
a0e07ba4 NJ |
311 | |
312 | If more than one of these options is specified for the same slot, the | |
313 | order of precedence, highest first is | |
314 | ||
315 | @itemize @bullet | |
316 | @item | |
317 | @code{#:init-keyword}, if @var{init-keyword} is present in the options | |
318 | passed to @code{make} | |
319 | ||
320 | @item | |
321 | @code{#:init-thunk}, @code{#:init-form} or @code{#:init-value}. | |
322 | @end itemize | |
323 | ||
324 | If the slot definition contains more than one initialization option of | |
325 | the same precedence, the later ones are ignored. If a slot is not | |
326 | initialized at all, its value is unbound. | |
327 | ||
328 | In general, slots that are shared between more than one instance are | |
329 | only initialized at new instance creation time if the slot value is | |
330 | unbound at that time. However, if the new instance creation specifies | |
331 | a valid init keyword and value for a shared slot, the slot is | |
332 | re-initialized regardless of its previous value. | |
333 | ||
334 | Note, however, that the power of GOOPS' metaobject protocol means that | |
335 | everything written here may be customized or overridden for particular | |
336 | classes! The slot initializations described here are performed by the least | |
337 | specialized method of the generic function @code{initialize}, whose | |
338 | signature is | |
339 | ||
340 | @example | |
341 | (define-method (initialize (object <object>) initargs) ...) | |
342 | @end example | |
343 | ||
344 | The initialization of instances of any given class can be customized by | |
345 | defining a @code{initialize} method that is specialized for that class, | |
346 | and the author of the specialized method may decide to call | |
347 | @code{next-method} - which will result in a call to the next less | |
348 | specialized @code{initialize} method - at any point within the | |
349 | specialized code, or maybe not at all. In general, therefore, the | |
350 | initialization mechanisms described here may be modified or overridden by | |
351 | more specialized code, or may not be supported at all for particular | |
352 | classes. | |
353 | @end deffn | |
354 | ||
8445eb1d NJ |
355 | @deffn {slot option} #:getter getter |
356 | @deffnx {slot option} #:setter setter | |
357 | @deffnx {slot option} #:accessor accessor | |
358 | Given an object @var{obj} with slots named @code{foo} and @code{bar}, it | |
359 | is always possible to read and write those slots by calling | |
360 | @code{slot-ref} and @code{slot-set!} with the relevant slot name; for | |
361 | example: | |
362 | ||
363 | @example | |
364 | (slot-ref @var{obj} 'foo) | |
365 | (slot-set! @var{obj} 'bar 25) | |
366 | @end example | |
367 | ||
368 | The @code{#:getter}, @code{#:setter} and @code{#:accessor} options, if | |
369 | present, tell GOOPS to create generic function and method definitions | |
370 | that can be used to get and set the slot value more conveniently. | |
371 | @var{getter} specifies a generic function to which GOOPS will add a | |
372 | method for getting the slot value. @var{setter} specifies a generic | |
373 | function to which GOOPS will add a method for setting the slot value. | |
374 | @var{accessor} specifies an accessor to which GOOPS will add methods for | |
375 | both getting and setting the slot value. | |
376 | ||
377 | So if a class includes a slot definition like this: | |
378 | ||
379 | @example | |
380 | (c #:getter get-count #:setter set-count #:accessor count) | |
381 | @end example | |
382 | ||
383 | GOOPS defines generic function methods such that the slot value can be | |
384 | referenced using either the getter or the accessor - | |
385 | ||
386 | @example | |
387 | (let ((current-count (get-count obj))) @dots{}) | |
388 | (let ((current-count (count obj))) @dots{}) | |
389 | @end example | |
390 | ||
391 | - and set using either the setter or the accessor - | |
392 | ||
393 | @example | |
394 | (set-count obj (+ 1 current-count)) | |
395 | (set! (count obj) (+ 1 current-count)) | |
396 | @end example | |
397 | ||
398 | Note that | |
399 | ||
400 | @itemize @bullet | |
401 | @item | |
402 | with an accessor, the slot value is set using the generalized | |
403 | @code{set!} syntax | |
404 | ||
405 | @item | |
406 | in practice, it is unusual for a slot to use all three of these options: | |
407 | read-only, write-only and read-write slots would typically use only | |
408 | @code{#:getter}, @code{#:setter} and @code{#:accessor} options | |
409 | respectively. | |
410 | @end itemize | |
411 | ||
412 | If the specified names are already bound in the top-level environment to | |
413 | values that cannot be upgraded to generic functions, those values are | |
414 | overwritten during evaluation of the @code{define-class} that contains | |
415 | the slot definition. For details, see @ref{Generic Function Internals,, | |
416 | ensure-generic}. | |
417 | @end deffn | |
418 | ||
419 | @deffn {slot option} #:allocation allocation | |
420 | The @code{#:allocation} option tells GOOPS how to allocate storage for | |
421 | the slot. Possible values for @var{allocation} are | |
422 | ||
423 | @itemize @bullet | |
424 | @item @code{#:instance} | |
425 | ||
426 | Indicates that GOOPS should create separate storage for this slot in | |
427 | each new instance of the containing class (and its subclasses). | |
428 | ||
429 | @item @code{#:class} | |
430 | ||
431 | Indicates that GOOPS should create storage for this slot that is shared | |
432 | by all instances of the containing class (and its subclasses). In other | |
433 | words, a slot in class @var{C} with allocation @code{#:class} is shared | |
434 | by all @var{instance}s for which @code{(is-a? @var{instance} @var{c})}. | |
435 | ||
436 | @item @code{#:each-subclass} | |
437 | ||
438 | Indicates that GOOPS should create storage for this slot that is shared | |
439 | by all @emph{direct} instances of the containing class, and that | |
440 | whenever a subclass of the containing class is defined, GOOPS should | |
441 | create a new storage for the slot that is shared by all @emph{direct} | |
442 | instances of the subclass. In other words, a slot with allocation | |
443 | @code{#:each-subclass} is shared by all instances with the same | |
444 | @code{class-of}. | |
445 | ||
446 | @item @code{#:virtual} | |
447 | ||
448 | Indicates that GOOPS should not allocate storage for this slot. The | |
449 | slot definition must also include the @code{#:slot-ref} and | |
450 | @code{#:slot-set!} options to specify how to reference and set the value | |
451 | for this slot. | |
452 | @end itemize | |
453 | ||
454 | The default value is @code{#:instance}. | |
455 | ||
456 | Slot allocation options are processed when defining a new class by the | |
457 | generic function @code{compute-get-n-set}, which is specialized by the | |
458 | class's metaclass. Hence new types of slot allocation can be | |
459 | implemented by defining a new metaclass and a method for | |
460 | @code{compute-get-n-set} that is specialized for the new metaclass. For | |
461 | an example of how to do this, see @ref{Customizing Class Definition}. | |
462 | @end deffn | |
463 | ||
464 | @deffn {slot option} #:slot-ref getter | |
465 | @deffnx {slot option} #:slot-set! setter | |
466 | The @code{#:slot-ref} and @code{#:slot-set!} options must be specified | |
467 | if the slot allocation is @code{#:virtual}, and are ignored otherwise. | |
468 | ||
469 | @var{getter} should be a closure taking a single @var{instance} parameter | |
470 | that returns the current slot value. @var{setter} should be a closure | |
471 | taking two parameters - @var{instance} and @var{new-val} - that sets the | |
472 | slot value to @var{new-val}. | |
473 | @end deffn | |
474 | ||
475 | @node Class Options | |
476 | @subsection Class Options | |
477 | ||
478 | @deffn {class option} #:metaclass metaclass | |
479 | The @code{#:metaclass} class option specifies the metaclass of the class | |
480 | being defined. @var{metaclass} must be a class that inherits from | |
481 | @code{<class>}. For the use of metaclasses, see @ref{Metaobjects and | |
482 | the Metaobject Protocol} and @ref{Terminology}. | |
483 | ||
484 | If the @code{#:metaclass} option is absent, GOOPS reuses or constructs a | |
485 | metaclass for the new class by calling @code{ensure-metaclass} | |
486 | (@pxref{Class Definition Internals,, ensure-metaclass}). | |
487 | @end deffn | |
488 | ||
489 | @deffn {class option} #:name name | |
490 | The @code{#:name} class option specifies the new class's name. This | |
491 | name is used to identify the class whenever related objects - the class | |
492 | itself, its instances and its subclasses - are printed. | |
493 | ||
494 | If the @code{#:name} option is absent, GOOPS uses the first argument to | |
495 | @code{define-class} as the class name. | |
496 | @end deffn | |
497 | ||
a0e07ba4 | 498 | @node Class Definition Internals |
c34d74ff | 499 | @subsection Class Definition Internals |
a0e07ba4 NJ |
500 | |
501 | Implementation notes: @code{define-class} expands to an expression which | |
502 | ||
503 | @itemize @bullet | |
504 | @item | |
505 | checks that it is being evaluated only at top level | |
506 | ||
507 | @item | |
508 | defines any accessors that are implied by the @var{slot-definition}s | |
509 | ||
510 | @item | |
511 | uses @code{class} to create the new class (@pxref{Class Definition | |
512 | Internals,, class}) | |
513 | ||
514 | @item | |
515 | checks for a previous class definition for @var{name} and, if found, | |
516 | handles the redefinition by invoking @code{class-redefinition} | |
517 | (@pxref{Redefining a Class}). | |
518 | @end itemize | |
519 | ||
520 | @deffn syntax class name (super @dots{}) slot-definition @dots{} . options | |
521 | Return a newly created class that inherits from @var{super}s, with | |
522 | direct slots defined by @var{slot-definition}s and class options | |
523 | @var{options}. For the format of @var{slot-definition}s and | |
8445eb1d | 524 | @var{options}, see @ref{Defining New Classes,, define-class}. |
a0e07ba4 NJ |
525 | @end deffn |
526 | ||
527 | Implementation notes: @code{class} expands to an expression which | |
528 | ||
529 | @itemize @bullet | |
530 | @item | |
531 | processes the class and slot definition options to check that they are | |
532 | well-formed, to convert the @code{#:init-form} option to an | |
533 | @code{#:init-thunk} option, to supply a default environment parameter | |
534 | (the current top-level environment) and to evaluate all the bits that | |
535 | need to be evaluated | |
536 | ||
537 | @item | |
538 | calls @code{make-class} to create the class with the processed and | |
539 | evaluated parameters. | |
540 | @end itemize | |
541 | ||
542 | @deffn procedure make-class supers slots . options | |
543 | Return a newly created class that inherits from @var{supers}, with | |
544 | direct slots defined by @var{slots} and class options @var{options}. | |
8445eb1d NJ |
545 | For the format of @var{slots} and @var{options}, see @ref{Defining New |
546 | Classes,, define-class}, except note that for @code{make-class}, | |
a0e07ba4 NJ |
547 | @var{slots} and @var{options} are separate list parameters: @var{slots} |
548 | here is a list of slot definitions. | |
549 | @end deffn | |
550 | ||
551 | Implementation notes: @code{make-class} | |
552 | ||
553 | @itemize @bullet | |
554 | @item | |
555 | adds @code{<object>} to the @var{supers} list if @var{supers} is empty | |
556 | or if none of the classes in @var{supers} have @code{<object>} in their | |
557 | class precedence list | |
558 | ||
559 | @item | |
560 | defaults the @code{#:environment}, @code{#:name} and @code{#:metaclass} | |
561 | options, if they are not specified by @var{options}, to the current | |
562 | top-level environment, the unbound value, and @code{(ensure-metaclass | |
563 | @var{supers})} respectively (@pxref{Class Definition Internals,, | |
564 | ensure-metaclass}) | |
565 | ||
566 | @item | |
567 | checks for duplicate classes in @var{supers} and duplicate slot names in | |
568 | @var{slots}, and signals an error if there are any duplicates | |
569 | ||
570 | @item | |
571 | calls @code{make}, passing the metaclass as the first parameter and all | |
572 | other parameters as option keywords with values. | |
573 | @end itemize | |
574 | ||
575 | @deffn procedure ensure-metaclass supers env | |
576 | Return a metaclass suitable for a class that inherits from the list of | |
577 | classes in @var{supers}. The returned metaclass is the union by | |
578 | inheritance of the metaclasses of the classes in @var{supers}. | |
579 | ||
580 | In the simplest case, where all the @var{supers} are straightforward | |
581 | classes with metaclass @code{<class>}, the returned metaclass is just | |
582 | @code{<class>}. | |
583 | ||
584 | For a more complex example, suppose that @var{supers} contained one | |
585 | class with metaclass @code{<operator-class>} and one with metaclass | |
586 | @code{<foreign-object-class>}. Then the returned metaclass would be a | |
587 | class that inherits from both @code{<operator-class>} and | |
588 | @code{<foreign-object-class>}. | |
589 | ||
590 | If @var{supers} is the empty list, @code{ensure-metaclass} returns the | |
591 | default GOOPS metaclass @code{<class>}. | |
592 | ||
593 | GOOPS keeps a list of the metaclasses created by | |
594 | @code{ensure-metaclass}, so that each required type of metaclass only | |
595 | has to be created once. | |
596 | ||
597 | The @code{env} parameter is ignored. | |
598 | @end deffn | |
599 | ||
600 | @deffn procedure ensure-metaclass-with-supers meta-supers | |
601 | @code{ensure-metaclass-with-supers} is an internal procedure used by | |
602 | @code{ensure-metaclass} (@pxref{Class Definition Internals,, | |
603 | ensure-metaclass}). It returns a metaclass that is the union by | |
604 | inheritance of the metaclasses in @var{meta-supers}. | |
605 | @end deffn | |
606 | ||
607 | The internals of @code{make}, which is ultimately used to create the new | |
608 | class object, are described in @ref{Customizing Instance Creation}, | |
609 | which covers the creation and initialization of instances in general. | |
610 | ||
611 | @node Customizing Class Definition | |
c34d74ff | 612 | @subsection Customizing Class Definition |
a0e07ba4 NJ |
613 | |
614 | During the initialization of a new class, GOOPS calls a number of generic | |
615 | functions with the newly allocated class instance as the first | |
616 | argument. Specifically, GOOPS calls the generic function | |
617 | ||
618 | @itemize @bullet | |
619 | @item | |
620 | (initialize @var{class} @dots{}) | |
621 | @end itemize | |
622 | ||
623 | where @var{class} is the newly allocated class instance, and the default | |
624 | @code{initialize} method for arguments of type @code{<class>} calls the | |
625 | generic functions | |
626 | ||
627 | @itemize @bullet | |
628 | @item | |
629 | (compute-cpl @var{class}) | |
630 | ||
631 | @item | |
632 | (compute-slots @var{class}) | |
633 | ||
634 | @item | |
635 | (compute-get-n-set @var{class} @var{slot-def}), for each of the slot | |
636 | definitions returned by @code{compute-slots} | |
637 | ||
638 | @item | |
639 | (compute-getter-method @var{class} @var{slot-def}), for each of the | |
640 | slot definitions returned by @code{compute-slots} that includes a | |
641 | @code{#:getter} or @code{#:accessor} slot option | |
642 | ||
643 | @item | |
644 | (compute-setter-method @var{class} @var{slot-def}), for each of the | |
645 | slot definitions returned by @code{compute-slots} that includes a | |
646 | @code{#:setter} or @code{#:accessor} slot option. | |
647 | @end itemize | |
648 | ||
649 | If the metaclass of the new class is something more specialized than the | |
650 | default @code{<class>}, then the type of @var{class} in the calls above | |
651 | is more specialized than @code{<class>}, and hence it becomes possible | |
652 | to define generic function methods, specialized for the new class's | |
653 | metaclass, that can modify or override the default behaviour of | |
654 | @code{initialize}, @code{compute-cpl} or @code{compute-get-n-set}. | |
655 | ||
656 | @code{compute-cpl} computes the class precedence list (``CPL'') for the | |
657 | new class (@pxref{Class precedence list}), and returns it as a list of | |
658 | class objects. The CPL is important because it defines a superclass | |
659 | ordering that is used, when a generic function is invoked upon an | |
660 | instance of the class, to decide which of the available generic function | |
661 | methods is the most specific. Hence @code{compute-cpl} could be | |
662 | customized in order to modify the CPL ordering algorithm for all classes | |
663 | with a special metaclass. | |
664 | ||
665 | The default CPL algorithm is encapsulated by the @code{compute-std-cpl} | |
666 | procedure, which is in turn called by the default @code{compute-cpl} | |
667 | method. | |
668 | ||
669 | @deffn procedure compute-std-cpl class | |
670 | Compute and return the class precedence list for @var{class} according | |
671 | to the algorithm described in @ref{Class precedence list}. | |
672 | @end deffn | |
673 | ||
674 | @code{compute-slots} computes and returns a list of all slot definitions | |
675 | for the new class. By default, this list includes the direct slot | |
676 | definitions from the @code{define-class} form, plus the slot definitions | |
677 | that are inherited from the new class's superclasses. The default | |
678 | @code{compute-slots} method uses the CPL computed by @code{compute-cpl} | |
679 | to calculate this union of slot definitions, with the rule that slots | |
680 | inherited from superclasses are shadowed by direct slots with the same | |
681 | name. One possible reason for customizing @code{compute-slots} would be | |
682 | to implement an alternative resolution strategy for slot name conflicts. | |
683 | ||
684 | @code{compute-get-n-set} computes the low-level closures that will be | |
685 | used to get and set the value of a particular slot, and returns them in | |
686 | a list with two elements. | |
687 | ||
688 | The closures returned depend on how storage for that slot is allocated. | |
689 | The standard @code{compute-get-n-set} method, specialized for classes of | |
690 | type @code{<class>}, handles the standard GOOPS values for the | |
691 | @code{#:allocation} slot option (@pxref{Slot Options,, allocation}). By | |
692 | defining a new @code{compute-get-n-set} method for a more specialized | |
693 | metaclass, it is possible to support new types of slot allocation. | |
694 | ||
695 | Suppose you wanted to create a large number of instances of some class | |
696 | with a slot that should be shared between some but not all instances of | |
697 | that class - say every 10 instances should share the same slot storage. | |
698 | The following example shows how to implement and use a new type of slot | |
699 | allocation to do this. | |
700 | ||
701 | @example | |
702 | (define-class <batched-allocation-metaclass> (<class>)) | |
703 | ||
704 | (let ((batch-allocation-count 0) | |
705 | (batch-get-n-set #f)) | |
45867c2a NJ |
706 | (define-method (compute-get-n-set |
707 | (class <batched-allocation-metaclass>) s) | |
a0e07ba4 NJ |
708 | (case (slot-definition-allocation s) |
709 | ((#:batched) | |
710 | ;; If we've already used the same slot storage for 10 instances, | |
711 | ;; reset variables. | |
712 | (if (= batch-allocation-count 10) | |
713 | (begin | |
714 | (set! batch-allocation-count 0) | |
715 | (set! batch-get-n-set #f))) | |
716 | ;; If we don't have a current pair of get and set closures, | |
717 | ;; create one. make-closure-variable returns a pair of closures | |
718 | ;; around a single Scheme variable - see goops.scm for details. | |
719 | (or batch-get-n-set | |
720 | (set! batch-get-n-set (make-closure-variable))) | |
721 | ;; Increment the batch allocation count. | |
722 | (set! batch-allocation-count (+ batch-allocation-count 1)) | |
723 | batch-get-n-set) | |
724 | ||
725 | ;; Call next-method to handle standard allocation types. | |
726 | (else (next-method))))) | |
727 | ||
728 | (define-class <class-using-batched-slot> () | |
729 | ... | |
730 | (c #:allocation #:batched) | |
731 | ... | |
732 | #:metaclass <batched-allocation-metaclass>) | |
ddee39a1 | 733 | @end example |
a0e07ba4 NJ |
734 | |
735 | The usage of @code{compute-getter-method} and @code{compute-setter-method} | |
736 | is described in @ref{MOP Specification}. | |
737 | ||
738 | @code{compute-cpl} and @code{compute-get-n-set} are called by the | |
739 | standard @code{initialize} method for classes whose metaclass is | |
740 | @code{<class>}. But @code{initialize} itself can also be modified, by | |
741 | defining an @code{initialize} method specialized to the new class's | |
742 | metaclass. Such a method could complete override the standard | |
743 | behaviour, by not calling @code{(next-method)} at all, but more | |
744 | typically it would perform additional class initialization steps before | |
745 | and/or after calling @code{(next-method)} for the standard behaviour. | |
746 | ||
747 | @node STKlos Compatibility | |
c34d74ff | 748 | @subsection STKlos Compatibility |
a0e07ba4 NJ |
749 | |
750 | If the STKlos compatibility module is loaded, @code{define-class} is | |
751 | overwritten by a STKlos-specific definition; the standard GOOPS | |
752 | definition of @code{define-class} remains available in | |
753 | @code{standard-define-class}. | |
754 | ||
755 | @deffn syntax standard-define-class name (super @dots{}) slot-definition @dots{} . options | |
756 | @code{standard-define-class} is equivalent to the standard GOOPS | |
757 | @code{define-class}. | |
758 | @end deffn | |
759 | ||
760 | @node Creating Instances | |
c34d74ff | 761 | @section Creating Instances |
a0e07ba4 NJ |
762 | |
763 | @menu | |
764 | * Basic Instance Creation:: | |
765 | * Customizing Instance Creation:: | |
766 | @end menu | |
767 | ||
768 | @node Basic Instance Creation | |
c34d74ff | 769 | @subsection Basic Instance Creation |
a0e07ba4 NJ |
770 | |
771 | To create a new instance of any GOOPS class, use the generic function | |
772 | @code{make} or @code{make-instance}, passing the required class and any | |
773 | appropriate instance initialization arguments as keyword and value | |
774 | pairs. Note that @code{make} and @code{make-instances} are aliases for | |
775 | each other - their behaviour is identical. | |
776 | ||
777 | @deffn generic make | |
778 | @deffnx method make (class <class>) . initargs | |
779 | Create and return a new instance of class @var{class}, initialized using | |
780 | @var{initargs}. | |
781 | ||
782 | In theory, @var{initargs} can have any structure that is understood by | |
783 | whatever methods get applied when the @code{initialize} generic function | |
784 | is applied to the newly allocated instance. | |
785 | ||
786 | In practice, specialized @code{initialize} methods would normally call | |
787 | @code{(next-method)}, and so eventually the standard GOOPS | |
788 | @code{initialize} methods are applied. These methods expect | |
789 | @var{initargs} to be a list with an even number of elements, where | |
790 | even-numbered elements (counting from zero) are keywords and | |
791 | odd-numbered elements are the corresponding values. | |
792 | ||
793 | GOOPS processes initialization argument keywords automatically for slots | |
794 | whose definition includes the @code{#:init-keyword} option (@pxref{Slot | |
795 | Options,, init-keyword}). Other keyword value pairs can only be | |
796 | processed by an @code{initialize} method that is specialized for the new | |
797 | instance's class. Any unprocessed keyword value pairs are ignored. | |
798 | @end deffn | |
799 | ||
800 | @deffn generic make-instance | |
801 | @deffnx method make-instance (class <class>) . initargs | |
802 | @code{make-instance} is an alias for @code{make}. | |
803 | @end deffn | |
804 | ||
805 | @node Customizing Instance Creation | |
c34d74ff | 806 | @subsection Customizing Instance Creation |
a0e07ba4 NJ |
807 | |
808 | @code{make} itself is a generic function. Hence the @code{make} | |
809 | invocation itself can be customized in the case where the new instance's | |
810 | metaclass is more specialized than the default @code{<class>}, by | |
811 | defining a @code{make} method that is specialized to that metaclass. | |
812 | ||
813 | Normally, however, the method for classes with metaclass @code{<class>} | |
814 | will be applied. This method calls two generic functions: | |
815 | ||
816 | @itemize @bullet | |
817 | @item | |
818 | (allocate-instance @var{class} . @var{initargs}) | |
819 | ||
820 | @item | |
821 | (initialize @var{instance} . @var{initargs}) | |
822 | @end itemize | |
823 | ||
824 | @code{allocate-instance} allocates storage for and returns the new | |
825 | instance, uninitialized. You might customize @code{allocate-instance}, | |
826 | for example, if you wanted to provide a GOOPS wrapper around some other | |
827 | object programming system. | |
828 | ||
829 | To do this, you would create a specialized metaclass, which would act as | |
830 | the metaclass for all classes and instances from the other system. Then | |
831 | define an @code{allocate-instance} method, specialized to that | |
832 | metaclass, which calls a Guile primitive C function, which in turn | |
833 | allocates the new instance using the interface of the other object | |
834 | system. | |
835 | ||
836 | In this case, for a complete system, you would also need to customize a | |
837 | number of other generic functions like @code{make} and | |
838 | @code{initialize}, so that GOOPS knows how to make classes from the | |
839 | other system, access instance slots, and so on. | |
840 | ||
841 | @code{initialize} initializes the instance that is returned by | |
842 | @code{allocate-instance}. The standard GOOPS methods perform | |
843 | initializations appropriate to the instance class. | |
844 | ||
845 | @itemize @bullet | |
846 | @item | |
847 | At the least specialized level, the method for instances of type | |
848 | @code{<object>} performs internal GOOPS instance initialization, and | |
849 | initializes the instance's slots according to the slot definitions and | |
850 | any slot initialization keywords that appear in @var{initargs}. | |
851 | ||
852 | @item | |
853 | The method for instances of type @code{<class>} calls | |
854 | @code{(next-method)}, then performs the class initializations described | |
855 | in @ref{Customizing Class Definition}. | |
856 | ||
857 | @item | |
858 | and so on for generic functions, method, operator classes @dots{} | |
859 | @end itemize | |
860 | ||
861 | Similarly, you can customize the initialization of instances of any | |
862 | application-defined class by defining an @code{initialize} method | |
863 | specialized to that class. | |
864 | ||
865 | Imagine a class whose instances' slots need to be initialized at | |
866 | instance creation time by querying a database. Although it might be | |
867 | possible to achieve this a combination of @code{#:init-thunk} keywords | |
868 | and closures in the slot definitions, it is neater to write an | |
869 | @code{initialize} method for the class that queries the database once | |
870 | and initializes all the dependent slot values according to the results. | |
871 | ||
872 | @node Accessing Slots | |
c34d74ff | 873 | @section Accessing Slots |
a0e07ba4 NJ |
874 | |
875 | The definition of a slot contains at the very least a slot name, and may | |
876 | also contain various slot options, including getter, setter and/or | |
877 | accessor functions for the slot. | |
878 | ||
879 | It is always possible to access slots by name, using the various | |
880 | ``slot-ref'' and ``slot-set!'' procedures described in the following | |
eb12b401 | 881 | subsubsections. For example, |
a0e07ba4 NJ |
882 | |
883 | @example | |
884 | (define-class <my-class> () ;; Define a class with slots | |
885 | (count #:init-value 0) ;; named "count" and "cache". | |
886 | (cache #:init-value '()) | |
887 | @dots{}) | |
888 | ||
889 | (define inst (make <my-class>)) ;; Make an instance of this class. | |
890 | ||
891 | (slot-set! inst 'count 5) ;; Set the value of the "count" | |
892 | ;; slot to 5. | |
893 | ||
894 | (slot-set! inst 'cache ;; Modify the value of the | |
895 | (cons (cons "^it" "It") ;; "cache" slot. | |
896 | (slot-ref inst 'cache))) | |
897 | @end example | |
898 | ||
899 | If a slot definition includes a getter, setter or accessor function, | |
900 | these can be used instead of @code{slot-ref} and @code{slot-set!} to | |
901 | access the slot. | |
902 | ||
903 | @example | |
904 | (define-class <adv-class> () ;; Define a new class whose slots | |
905 | (count #:setter set-count) ;; use a getter, a setter and | |
906 | (cache #:accessor cache) ;; an accessor. | |
907 | (csize #:getter cache-size) | |
908 | @dots{}) | |
909 | ||
910 | (define inst (make <adv-class>)) ;; Make an instance of this class. | |
911 | ||
912 | (set-count inst 5) ;; Set the value of the "count" | |
913 | ;; slot to 5. | |
914 | ||
915 | (set! (cache inst) ;; Modify the value of the | |
916 | (cons (cons "^it" "It") ;; "cache" slot. | |
917 | (cache inst))) | |
918 | ||
919 | (let ((size (cache-size inst))) ;; Get the value of the "csize" | |
920 | @dots{}) ;; slot. | |
921 | @end example | |
922 | ||
923 | Whichever of these methods is used to access slots, GOOPS always calls | |
924 | the low-level @dfn{getter} and @dfn{setter} closures for the slot to get | |
925 | and set its value. These closures make sure that the slot behaves | |
926 | according to the @code{#:allocation} type that was specified in the slot | |
927 | definition (@pxref{Slot Options,, allocation}). (For more about these | |
928 | closures, see @ref{Customizing Class Definition,, compute-get-n-set}.) | |
929 | ||
930 | @menu | |
931 | * Instance Slots:: | |
932 | * Class Slots:: | |
933 | * Handling Slot Access Errors:: | |
934 | @end menu | |
935 | ||
936 | @node Instance Slots | |
c34d74ff | 937 | @subsection Instance Slots |
a0e07ba4 NJ |
938 | |
939 | Any slot, regardless of its allocation, can be queried, referenced and | |
940 | set using the following four primitive procedures. | |
941 | ||
942 | @deffn {primitive procedure} slot-exists? obj slot-name | |
943 | Return @code{#t} if @var{obj} has a slot with name @var{slot-name}, | |
944 | otherwise @code{#f}. | |
945 | @end deffn | |
946 | ||
947 | @deffn {primitive procedure} slot-bound? obj slot-name | |
948 | Return @code{#t} if the slot named @var{slot-name} in @var{obj} has a | |
949 | value, otherwise @code{#f}. | |
950 | ||
951 | @code{slot-bound?} calls the generic function @code{slot-missing} if | |
952 | @var{obj} does not have a slot called @var{slot-name} (@pxref{Handling | |
953 | Slot Access Errors, slot-missing}). | |
954 | @end deffn | |
955 | ||
956 | @deffn {primitive procedure} slot-ref obj slot-name | |
957 | Return the value of the slot named @var{slot-name} in @var{obj}. | |
958 | ||
959 | @code{slot-ref} calls the generic function @code{slot-missing} if | |
960 | @var{obj} does not have a slot called @var{slot-name} (@pxref{Handling | |
961 | Slot Access Errors, slot-missing}). | |
962 | ||
963 | @code{slot-ref} calls the generic function @code{slot-unbound} if the | |
964 | named slot in @var{obj} does not have a value (@pxref{Handling Slot | |
965 | Access Errors, slot-unbound}). | |
966 | @end deffn | |
967 | ||
968 | @deffn {primitive procedure} slot-set! obj slot-name value | |
969 | Set the value of the slot named @var{slot-name} in @var{obj} to @var{value}. | |
970 | ||
971 | @code{slot-set!} calls the generic function @code{slot-missing} if | |
972 | @var{obj} does not have a slot called @var{slot-name} (@pxref{Handling | |
973 | Slot Access Errors, slot-missing}). | |
974 | @end deffn | |
975 | ||
976 | GOOPS stores information about slots in class metaobjects. Internally, | |
977 | all of these procedures work by looking up the slot definition for the | |
978 | slot named @var{slot-name} in the class metaobject for @code{(class-of | |
979 | @var{obj})}, and then using the slot definition's ``getter'' and | |
980 | ``setter'' closures to get and set the slot value. | |
981 | ||
982 | The next four procedures differ from the previous ones in that they take | |
983 | the class metaobject as an explicit argument, rather than assuming | |
984 | @code{(class-of @var{obj})}. Therefore they allow you to apply the | |
985 | ``getter'' and ``setter'' closures of a slot definition in one class to | |
986 | an instance of a different class. | |
987 | ||
988 | [ *fixme* I have no idea why this is useful! Perhaps when a slot in | |
989 | @code{(class-of @var{obj})} shadows a slot with the same name in one of | |
990 | its superclasses? There should be an enlightening example here. ] | |
991 | ||
992 | @deffn {primitive procedure} slot-exists-using-class? class obj slot-name | |
993 | Return @code{#t} if the class metaobject @var{class} has a slot | |
994 | definition for a slot with name @var{slot-name}, otherwise @code{#f}. | |
995 | @end deffn | |
996 | ||
997 | @deffn {primitive procedure} slot-bound-using-class? class obj slot-name | |
998 | Return @code{#t} if applying @code{slot-ref-using-class} to the same | |
999 | arguments would call the generic function @code{slot-unbound}, otherwise | |
1000 | @code{#f}. | |
1001 | ||
1002 | @code{slot-bound-using-class?} calls the generic function | |
1003 | @code{slot-missing} if @var{class} does not have a slot definition for a | |
1004 | slot called @var{slot-name} (@pxref{Handling Slot Access Errors, | |
1005 | slot-missing}). | |
1006 | @end deffn | |
1007 | ||
1008 | @deffn {primitive procedure} slot-ref-using-class class obj slot-name | |
1009 | Apply the ``getter'' closure for the slot named @var{slot-name} in | |
1010 | @var{class} to @var{obj}, and return its result. | |
1011 | ||
1012 | @code{slot-ref-using-class} calls the generic function | |
1013 | @code{slot-missing} if @var{class} does not have a slot definition for a | |
1014 | slot called @var{slot-name} (@pxref{Handling Slot Access Errors, | |
1015 | slot-missing}). | |
1016 | ||
1017 | @code{slot-ref-using-class} calls the generic function | |
1018 | @code{slot-unbound} if the application of the ``getter'' closure to | |
1019 | @var{obj} returns an unbound value (@pxref{Handling Slot Access Errors, | |
1020 | slot-unbound}). | |
1021 | @end deffn | |
1022 | ||
1023 | @deffn {primitive procedure} slot-set-using-class! class obj slot-name value | |
1024 | Apply the ``setter'' closure for the slot named @var{slot-name} in | |
1025 | @var{class} to @var{obj} and @var{value}. | |
1026 | ||
1027 | @code{slot-set-using-class!} calls the generic function | |
1028 | @code{slot-missing} if @var{class} does not have a slot definition for a | |
1029 | slot called @var{slot-name} (@pxref{Handling Slot Access Errors, | |
1030 | slot-missing}). | |
1031 | @end deffn | |
1032 | ||
1033 | @node Class Slots | |
c34d74ff | 1034 | @subsection Class Slots |
a0e07ba4 NJ |
1035 | |
1036 | Slots whose allocation is per-class rather than per-instance can be | |
1037 | referenced and set without needing to specify any particular instance. | |
1038 | ||
1039 | @deffn procedure class-slot-ref class slot-name | |
1040 | Return the value of the slot named @var{slot-name} in class @var{class}. | |
1041 | The named slot must have @code{#:class} or @code{#:each-subclass} | |
1042 | allocation (@pxref{Slot Options,, allocation}). | |
1043 | ||
1044 | If there is no such slot with @code{#:class} or @code{#:each-subclass} | |
1045 | allocation, @code{class-slot-ref} calls the @code{slot-missing} generic | |
1046 | function with arguments @var{class} and @var{slot-name}. Otherwise, if | |
1047 | the slot value is unbound, @code{class-slot-ref} calls the | |
1048 | @code{slot-missing} generic function, with the same arguments. | |
1049 | @end deffn | |
1050 | ||
1051 | @deffn procedure class-slot-set! class slot-name value | |
1052 | Set the value of the slot named @var{slot-name} in class @var{class} to | |
1053 | @var{value}. The named slot must have @code{#:class} or | |
1054 | @code{#:each-subclass} allocation (@pxref{Slot Options,, allocation}). | |
1055 | ||
1056 | If there is no such slot with @code{#:class} or @code{#:each-subclass} | |
1057 | allocation, @code{class-slot-ref} calls the @code{slot-missing} generic | |
1058 | function with arguments @var{class} and @var{slot-name}. | |
1059 | @end deffn | |
1060 | ||
1061 | @node Handling Slot Access Errors | |
c34d74ff | 1062 | @subsection Handling Slot Access Errors |
a0e07ba4 NJ |
1063 | |
1064 | GOOPS calls one of the following generic functions when a ``slot-ref'' | |
1065 | or ``slot-set!'' call specifies a non-existent slot name, or tries to | |
1066 | reference a slot whose value is unbound. | |
1067 | ||
1068 | @deffn generic slot-missing | |
1069 | @deffnx method slot-missing (class <class>) slot-name | |
1070 | @deffnx method slot-missing (class <class>) (object <object>) slot-name | |
1071 | @deffnx method slot-missing (class <class>) (object <object>) slot-name value | |
1072 | When an application attempts to reference or set a class or instance | |
1073 | slot by name, and the slot name is invalid for the specified @var{class} | |
1074 | or @var{object}, GOOPS calls the @code{slot-missing} generic function. | |
1075 | ||
1076 | The default methods all call @code{goops-error} with an appropriate | |
1077 | message. | |
1078 | @end deffn | |
1079 | ||
1080 | @deffn generic slot-unbound | |
1081 | @deffnx method slot-unbound (object <object>) | |
1082 | @deffnx method slot-unbound (class <class>) slot-name | |
1083 | @deffnx method slot-unbound (class <class>) (object <object>) slot-name | |
1084 | When an application attempts to reference a class or instance slot, and | |
1085 | the slot's value is unbound, GOOPS calls the @code{slot-unbound} generic | |
1086 | function. | |
1087 | ||
1088 | The default methods all call @code{goops-error} with an appropriate | |
1089 | message. | |
1090 | @end deffn | |
1091 | ||
1092 | @node Creating Generic Functions | |
c34d74ff | 1093 | @section Creating Generic Functions |
a0e07ba4 NJ |
1094 | |
1095 | A generic function is a collection of methods, with rules for | |
1096 | determining which of the methods should be applied for any given | |
1097 | invocation of the generic function. | |
1098 | ||
1099 | GOOPS represents generic functions as metaobjects of the class | |
1100 | @code{<generic>} (or one of its subclasses). | |
1101 | ||
1102 | @menu | |
1103 | * Basic Generic Function Creation:: | |
1104 | * Generic Function Internals:: | |
1105 | * Extending Guiles Primitives:: | |
1106 | @end menu | |
1107 | ||
1108 | @node Basic Generic Function Creation | |
c34d74ff | 1109 | @subsection Basic Generic Function Creation |
a0e07ba4 NJ |
1110 | |
1111 | The following forms may be used to bind a variable to a generic | |
1112 | function. Depending on that variable's pre-existing value, the generic | |
1113 | function may be created empty - with no methods - or it may contain | |
1114 | methods that are inferred from the pre-existing value. | |
1115 | ||
1116 | It is not, in general, necessary to use @code{define-generic} or | |
1117 | @code{define-accessor} before defining methods for the generic function | |
1118 | using @code{define-method}, since @code{define-method} will | |
1119 | automatically interpolate a @code{define-generic} call, or upgrade an | |
1120 | existing generic to an accessor, if that is implied by the | |
1121 | @code{define-method} call. Note in particular that, | |
1122 | if the specified variable already has a @emph{generic function} value, | |
1123 | @code{define-generic} and @code{define-accessor} will @emph{discard} it! | |
1124 | Obviously it is application-dependent whether this is desirable or not. | |
1125 | ||
1126 | If, for example, you wanted to extend @code{+} for a class representing | |
1127 | a new numerical type, you probably want to inherit any existing methods | |
1128 | for @code{+} and so should not use @code{define-generic}. If, on the | |
1129 | other hand, you do not want to risk inheriting methods whose behaviour | |
1130 | might surprise you, you can use @code{define-generic} or | |
1131 | @code{define-accessor} to wipe the slate clean. | |
1132 | ||
1133 | @deffn syntax define-generic symbol | |
1134 | Create a generic function with name @var{symbol} and bind it to the | |
1135 | variable @var{symbol}. | |
1136 | ||
1137 | If the variable @var{symbol} was previously bound to a Scheme procedure | |
1138 | (or procedure-with-setter), the old procedure (and setter) is | |
1139 | incorporated into the new generic function as its default procedure (and | |
1140 | setter). Any other previous value that was bound to @var{symbol}, | |
1141 | including an existing generic function, is overwritten by the new | |
1142 | generic function. | |
1143 | @end deffn | |
1144 | ||
1145 | @deffn syntax define-accessor symbol | |
1146 | Create an accessor with name @var{symbol} and bind it to the variable | |
1147 | @var{symbol}. | |
1148 | ||
1149 | If the variable @var{symbol} was previously bound to a Scheme procedure | |
1150 | (or procedure-with-setter), the old procedure (and setter) is | |
1151 | incorporated into the new accessor as its default procedure (and | |
1152 | setter). Any other previous value that was bound to @var{symbol}, | |
1153 | including an existing generic function or accessor, is overwritten by | |
1154 | the new definition. | |
1155 | @end deffn | |
1156 | ||
31a4ff3e MV |
1157 | It is sometimes tempting to use GOOPS accessors with short names. For |
1158 | example, it is tempting to use the name @code{x} for the x-coordinate | |
1159 | in vector packages. | |
1160 | ||
1161 | Assume that we work with a graphical package which needs to use two | |
1162 | independent vector packages for 2D and 3D vectors respectively. If | |
1163 | both packages export @code{x} we will encounter a name collision. | |
1164 | ||
1165 | This can be resolved automagically with the duplicates handler | |
1166 | @code{merge-generics} which gives the module system license to merge | |
1167 | all generic functions sharing a common name: | |
1168 | ||
aba0dff5 | 1169 | @lisp |
31a4ff3e | 1170 | (define-module (math 2D-vectors) |
8d9cb14e NJ |
1171 | #:use-module (oop goops) |
1172 | #:export (x y ...)) | |
31a4ff3e MV |
1173 | |
1174 | (define-module (math 3D-vectors) | |
8d9cb14e NJ |
1175 | #:use-module (oop goops) |
1176 | #:export (x y z ...)) | |
31a4ff3e MV |
1177 | |
1178 | (define-module (my-module) | |
8d9cb14e NJ |
1179 | #:use-module (math 2D-vectors) |
1180 | #:use-module (math 3D-vectors) | |
1181 | #:duplicates merge-generics) | |
aba0dff5 | 1182 | @end lisp |
31a4ff3e MV |
1183 | |
1184 | The generic function @code{x} in @code{(my-module)} will now share | |
1185 | methods with @code{x} in both imported modules. | |
1186 | ||
1187 | There will, in fact, now be three distinct generic functions named | |
1188 | @code{x}: @code{x} in @code{(2D-vectors)}, @code{x} in | |
1189 | @code{(3D-vectors)}, and @code{x} in @code{(my-module)}. The last | |
1190 | function will be an @code{<extended-generic>}, extending the previous | |
1191 | two functions. | |
1192 | ||
1193 | Let's call the imported generic functions the "ancestor functions". | |
1194 | The generic function @code{x} in @code{(my-module)} is, in turn, a | |
1195 | "descendant function" of the imported functions, extending its | |
1196 | ancestors. | |
1197 | ||
1198 | For any generic function G, the applicable methods are selected from | |
1199 | the union of the methods of the descendant functions, the methods of G | |
1200 | itself and the methods of the ancestor functions. | |
1201 | ||
1202 | This, ancestor functions share methods with their descendants and vice | |
1203 | versa. This implies that @code{x} in @code{(math 2D-vectors)} will | |
1204 | share the methods of @code{x} in @code{(my-module)} and vice versa, | |
1205 | while @code{x} in @code{(math 2D-vectors)} doesn't share the methods | |
1206 | of @code{x} in @code{(math 3D-vectors)}, thus preserving modularity. | |
1207 | ||
1208 | Sharing is dynamic, so that adding new methods to a descendant implies | |
1209 | adding it to the ancestor. | |
1210 | ||
1211 | If duplicates checking is desired in the above example, the following | |
8d9cb14e | 1212 | form of the @code{#:duplicates} option can be used instead: |
31a4ff3e | 1213 | |
aba0dff5 | 1214 | @lisp |
8d9cb14e | 1215 | #:duplicates (merge-generics check) |
aba0dff5 | 1216 | @end lisp |
31a4ff3e | 1217 | |
a0e07ba4 | 1218 | @node Generic Function Internals |
c34d74ff | 1219 | @subsection Generic Function Internals |
a0e07ba4 NJ |
1220 | |
1221 | @code{define-generic} calls @code{ensure-generic} to upgrade a | |
1222 | pre-existing procedure value, or @code{make} with metaclass | |
1223 | @code{<generic>} to create a new generic function. | |
1224 | ||
1225 | @code{define-accessor} calls @code{ensure-accessor} to upgrade a | |
1226 | pre-existing procedure value, or @code{make-accessor} to create a new | |
1227 | accessor. | |
1228 | ||
1229 | @deffn procedure ensure-generic old-definition [name] | |
1230 | Return a generic function with name @var{name}, if possible by using or | |
1231 | upgrading @var{old-definition}. If unspecified, @var{name} defaults to | |
1232 | @code{#f}. | |
1233 | ||
1234 | If @var{old-definition} is already a generic function, it is returned | |
1235 | unchanged. | |
1236 | ||
1237 | If @var{old-definition} is a Scheme procedure or procedure-with-setter, | |
1238 | @code{ensure-generic} returns a new generic function that uses | |
1239 | @var{old-definition} for its default procedure and setter. | |
1240 | ||
1241 | Otherwise @code{ensure-generic} returns a new generic function with no | |
1242 | defaults and no methods. | |
1243 | @end deffn | |
1244 | ||
1245 | @deffn procedure make-generic [name] | |
1246 | Return a new generic function with name @code{(car @var{name})}. If | |
1247 | unspecified, @var{name} defaults to @code{#f}. | |
1248 | @end deffn | |
1249 | ||
1250 | @code{ensure-generic} calls @code{make} with metaclasses | |
1251 | @code{<generic>} and @code{<generic-with-setter>}, depending on the | |
1252 | previous value of the variable that it is trying to upgrade. | |
1253 | ||
1254 | @code{make-generic} is a simple wrapper for @code{make} with metaclass | |
1255 | @code{<generic>}. | |
1256 | ||
1257 | @deffn procedure ensure-accessor proc [name] | |
1258 | Return an accessor with name @var{name}, if possible by using or | |
1259 | upgrading @var{proc}. If unspecified, @var{name} defaults to @code{#f}. | |
1260 | ||
1261 | If @var{proc} is already an accessor, it is returned unchanged. | |
1262 | ||
1263 | If @var{proc} is a Scheme procedure, procedure-with-setter or generic | |
1264 | function, @code{ensure-accessor} returns an accessor that reuses the | |
1265 | reusable elements of @var{proc}. | |
1266 | ||
1267 | Otherwise @code{ensure-accessor} returns a new accessor with no defaults | |
1268 | and no methods. | |
1269 | @end deffn | |
1270 | ||
1271 | @deffn procedure make-accessor [name] | |
1272 | Return a new accessor with name @code{(car @var{name})}. If | |
1273 | unspecified, @var{name} defaults to @code{#f}. | |
1274 | @end deffn | |
1275 | ||
1276 | @code{ensure-accessor} calls @code{make} with | |
1277 | metaclass @code{<generic-with-setter>}, as well as calls to | |
1278 | @code{ensure-generic}, @code{make-accessor} and (tail recursively) | |
1279 | @code{ensure-accessor}. | |
1280 | ||
1281 | @code{make-accessor} calls @code{make} twice, first | |
1282 | with metaclass @code{<generic>} to create a generic function for the | |
1283 | setter, then with metaclass @code{<generic-with-setter>} to create the | |
1284 | accessor, passing the setter generic function as the value of the | |
1285 | @code{#:setter} keyword. | |
1286 | ||
1287 | @node Extending Guiles Primitives | |
c34d74ff | 1288 | @subsection Extending Guile's Primitives |
a0e07ba4 NJ |
1289 | |
1290 | When GOOPS is loaded, many of Guile's primitive procedures can be | |
1291 | extended by giving them a generic function definition that operates | |
1292 | in conjunction with their normal C-coded implementation. For | |
1293 | primitives that are extended in this way, the result from the user- | |
1294 | or application-level point of view is that the extended primitive | |
1295 | behaves exactly like a generic function with the C-coded implementation | |
1296 | as its default method. | |
1297 | ||
1298 | The @code{generic-capability?} predicate should be used to determine | |
1299 | whether a particular primitive is extensible in this way. | |
1300 | ||
1301 | @deffn {primitive procedure} generic-capability? primitive | |
1302 | Return @code{#t} if @var{primitive} can be extended by giving it a | |
1303 | generic function definition, otherwise @code{#f}. | |
1304 | @end deffn | |
1305 | ||
1306 | Even when a primitive procedure is extensible like this, its generic | |
1307 | function definition is not created until it is needed by a call to | |
1308 | @code{define-method}, or until the application explicitly requests it | |
1309 | by calling @code{enable-primitive-generic!}. | |
1310 | ||
1311 | @deffn {primitive procedure} enable-primitive-generic! primitive | |
1312 | Force the creation of a generic function definition for | |
1313 | @var{primitive}. | |
1314 | @end deffn | |
1315 | ||
1316 | Once the generic function definition for a primitive has been created, | |
1317 | it can be retrieved using @code{primitive-generic-generic}. | |
1318 | ||
1319 | @deffn {primitive procedure} primitive-generic-generic primitive | |
1320 | Return the generic function definition of @var{primitive}. | |
1321 | ||
1322 | @code{primitive-generic-generic} raises an error if @var{primitive} | |
1323 | is not a primitive with generic capability, or if its generic capability | |
1324 | has not yet been enabled, whether implicitly (by @code{define-method}) | |
1325 | or explicitly (by @code{enable-primitive-generic!}). | |
1326 | @end deffn | |
1327 | ||
1328 | Note that the distinction between, on the one hand, primitives with | |
1329 | additional generic function definitions and, on the other hand, generic | |
1330 | functions with a default method, may disappear when GOOPS is fully | |
1331 | integrated into the core of Guile. Consequently, the | |
1332 | procedures described in this section may disappear as well. | |
1333 | ||
1334 | @node Adding Methods to Generic Functions | |
c34d74ff | 1335 | @section Adding Methods to Generic Functions |
a0e07ba4 NJ |
1336 | |
1337 | @menu | |
1338 | * Basic Method Definition:: | |
1339 | * Method Definition Internals:: | |
1340 | @end menu | |
1341 | ||
1342 | @node Basic Method Definition | |
c34d74ff | 1343 | @subsection Basic Method Definition |
a0e07ba4 NJ |
1344 | |
1345 | To add a method to a generic function, use the @code{define-method} form. | |
1346 | ||
1347 | @deffn syntax define-method (generic parameter @dots{}) . body | |
1348 | Define a method for the generic function or accessor @var{generic} with | |
1349 | parameters @var{parameter}s and body @var{body}. | |
1350 | ||
1351 | @var{generic} is a generic function. If @var{generic} is a variable | |
1352 | which is not yet bound to a generic function object, the expansion of | |
1353 | @code{define-method} will include a call to @code{define-generic}. If | |
1354 | @var{generic} is @code{(setter @var{generic-with-setter})}, where | |
1355 | @var{generic-with-setter} is a variable which is not yet bound to a | |
1356 | generic-with-setter object, the expansion will include a call to | |
1357 | @code{define-accessor}. | |
1358 | ||
1359 | Each @var{parameter} must be either a symbol or a two-element list | |
1360 | @code{(@var{symbol} @var{class})}. The symbols refer to variables in | |
1361 | the @var{body} that will be bound to the parameters supplied by the | |
1362 | caller when calling this method. The @var{class}es, if present, | |
1363 | specify the possible combinations of parameters to which this method | |
1364 | can be applied. | |
1365 | ||
1366 | @var{body} is the body of the method definition. | |
1367 | @end deffn | |
1368 | ||
1369 | @code{define-method} expressions look a little like normal Scheme | |
1370 | procedure definitions of the form | |
1371 | ||
1372 | @example | |
1373 | (define (name formals @dots{}) . body) | |
1374 | @end example | |
1375 | ||
1376 | The most important difference is that each formal parameter, apart from the | |
1377 | possible ``rest'' argument, can be qualified by a class name: | |
1378 | @code{@var{formal}} becomes @code{(@var{formal} @var{class})}. The | |
1379 | meaning of this qualification is that the method being defined | |
1380 | will only be applicable in a particular generic function invocation if | |
1381 | the corresponding argument is an instance of @code{@var{class}} (or one of | |
1382 | its subclasses). If more than one of the formal parameters is qualified | |
1383 | in this way, then the method will only be applicable if each of the | |
1384 | corresponding arguments is an instance of its respective qualifying class. | |
1385 | ||
1386 | Note that unqualified formal parameters act as though they are qualified | |
1387 | by the class @code{<top>}, which GOOPS uses to mean the superclass of | |
1388 | all valid Scheme types, including both primitive types and GOOPS classes. | |
1389 | ||
1390 | For example, if a generic function method is defined with | |
1391 | @var{parameter}s @code{((s1 <square>) (n <number>))}, that method is | |
1392 | only applicable to invocations of its generic function that have two | |
1393 | parameters where the first parameter is an instance of the | |
1394 | @code{<square>} class and the second parameter is a number. | |
1395 | ||
1396 | If a generic function is invoked with a combination of parameters for which | |
1397 | there is no applicable method, GOOPS raises an error. For more about | |
1398 | invocation error handling, and generic function invocation in general, | |
1399 | see @ref{Invoking Generic Functions}. | |
1400 | ||
1401 | @node Method Definition Internals | |
c34d74ff | 1402 | @subsection Method Definition Internals |
a0e07ba4 NJ |
1403 | |
1404 | @code{define-method} | |
1405 | ||
1406 | @itemize @bullet | |
1407 | @item | |
1408 | checks the form of the first parameter, and applies the following steps | |
1409 | to the accessor's setter if it has the @code{(setter @dots{})} form | |
1410 | ||
1411 | @item | |
1412 | interpolates a call to @code{define-generic} or @code{define-accessor} | |
1413 | if a generic function is not already defined with the supplied name | |
1414 | ||
1415 | @item | |
1416 | calls @code{method} with the @var{parameter}s and @var{body}, to make a | |
1417 | new method instance | |
1418 | ||
1419 | @item | |
1420 | calls @code{add-method!} to add this method to the relevant generic | |
1421 | function. | |
1422 | @end itemize | |
1423 | ||
1424 | @deffn syntax method (parameter @dots{}) . body | |
1425 | Make a method whose specializers are defined by the classes in | |
1426 | @var{parameter}s and whose procedure definition is constructed from the | |
1427 | @var{parameter} symbols and @var{body} forms. | |
1428 | ||
1429 | The @var{parameter} and @var{body} parameters should be as for | |
1430 | @code{define-method} (@pxref{Basic Method Definition,, define-method}). | |
1431 | @end deffn | |
1432 | ||
1433 | @code{method} | |
1434 | ||
1435 | @itemize @bullet | |
1436 | @item | |
1437 | extracts formals and specializing classes from the @var{parameter}s, | |
1438 | defaulting the class for unspecialized parameters to @code{<top>} | |
1439 | ||
1440 | @item | |
1441 | creates a closure using the formals and the @var{body} forms | |
1442 | ||
1443 | @item | |
1444 | calls @code{make} with metaclass @code{<method>} and the specializers | |
1445 | and closure using the @code{#:specializers} and @code{#:procedure} | |
1446 | keywords. | |
1447 | @end itemize | |
1448 | ||
1449 | @deffn procedure make-method specializers procedure | |
1450 | Make a method using @var{specializers} and @var{procedure}. | |
1451 | ||
1452 | @var{specializers} should be a list of classes that specifies the | |
1453 | parameter combinations to which this method will be applicable. | |
1454 | ||
1455 | @var{procedure} should be the closure that will applied to the generic | |
1456 | function parameters when this method is invoked. | |
1457 | @end deffn | |
1458 | ||
1459 | @code{make-method} is a simple wrapper around @code{make} with metaclass | |
1460 | @code{<method>}. | |
1461 | ||
1462 | @deffn generic add-method! target method | |
1463 | Generic function for adding method @var{method} to @var{target}. | |
1464 | @end deffn | |
1465 | ||
1466 | @deffn method add-method! (generic <generic>) (method <method>) | |
1467 | Add method @var{method} to the generic function @var{generic}. | |
1468 | @end deffn | |
1469 | ||
1470 | @deffn method add-method! (proc <procedure>) (method <method>) | |
1471 | If @var{proc} is a procedure with generic capability (@pxref{Extending | |
1472 | Guiles Primitives,, generic-capability?}), upgrade it to a | |
1473 | primitive generic and add @var{method} to its generic function | |
1474 | definition. | |
1475 | @end deffn | |
1476 | ||
1477 | @deffn method add-method! (pg <primitive-generic>) (method <method>) | |
1478 | Add method @var{method} to the generic function definition of @var{pg}. | |
1479 | ||
1480 | Implementation: @code{(add-method! (primitive-generic-generic pg) method)}. | |
1481 | @end deffn | |
1482 | ||
1483 | @deffn method add-method! (whatever <top>) (method <method>) | |
1484 | Raise an error indicating that @var{whatever} is not a valid generic | |
1485 | function. | |
1486 | @end deffn | |
1487 | ||
1488 | @node Invoking Generic Functions | |
c34d74ff | 1489 | @section Invoking Generic Functions |
a0e07ba4 NJ |
1490 | |
1491 | When a variable with a generic function definition appears as the first | |
1492 | element of a list that is being evaluated, the Guile evaluator tries | |
1493 | to apply the generic function to the arguments obtained by evaluating | |
1494 | the remaining elements of the list. [ *fixme* How do I put this in a | |
1495 | more Schemely and less Lispy way? ] | |
1496 | ||
1497 | Usually a generic function contains several method definitions, with | |
1498 | varying degrees of formal parameter specialization (@pxref{Basic | |
1499 | Method Definition,, define-method}). So it is necessary to sort these | |
1500 | methods by specificity with respect to the supplied arguments, and then | |
1501 | apply the most specific method definition. Less specific methods | |
1502 | may be applied subsequently if a method that is being applied calls | |
1503 | @code{next-method}. | |
1504 | ||
1505 | @menu | |
1506 | * Determining Which Methods to Apply:: | |
1507 | * Handling Invocation Errors:: | |
1508 | @end menu | |
1509 | ||
1510 | @node Determining Which Methods to Apply | |
c34d74ff | 1511 | @subsection Determining Which Methods to Apply |
a0e07ba4 NJ |
1512 | |
1513 | [ *fixme* Sorry - this is the area of GOOPS that I understand least of | |
1514 | all, so I'm afraid I have to pass on this section. Would some other | |
1515 | kind person consider filling it in? ] | |
1516 | ||
1517 | @deffn generic apply-generic | |
1518 | @deffnx method apply-generic (gf <generic>) args | |
1519 | @end deffn | |
1520 | ||
1521 | @deffn generic compute-applicable-methods | |
1522 | @deffnx method compute-applicable-methods (gf <generic>) args | |
1523 | @end deffn | |
1524 | ||
1525 | @deffn generic sort-applicable-methods | |
1526 | @deffnx method sort-applicable-methods (gf <generic>) methods args | |
1527 | @end deffn | |
1528 | ||
1529 | @deffn generic method-more-specific? | |
1530 | @deffnx method method-more-specific? (m1 <method>) (m2 <method>) args | |
1531 | @end deffn | |
1532 | ||
1533 | @deffn generic apply-method | |
1534 | @deffnx method apply-method (gf <generic>) methods build-next args | |
1535 | @end deffn | |
1536 | ||
1537 | @deffn generic apply-methods | |
1538 | @deffnx method apply-methods (gf <generic>) (l <list>) args | |
1539 | @end deffn | |
1540 | ||
1541 | @node Handling Invocation Errors | |
c34d74ff | 1542 | @subsection Handling Invocation Errors |
a0e07ba4 NJ |
1543 | |
1544 | @deffn generic no-method | |
1545 | @deffnx method no-method (gf <generic>) args | |
1546 | When an application invokes a generic function, and no methods at all | |
1547 | have been defined for that generic function, GOOPS calls the | |
1548 | @code{no-method} generic function. The default method calls | |
1549 | @code{goops-error} with an appropriate message. | |
1550 | @end deffn | |
1551 | ||
1552 | @deffn generic no-applicable-method | |
1553 | @deffnx method no-applicable-method (gf <generic>) args | |
1554 | When an application applies a generic function to a set of arguments, | |
1555 | and no methods have been defined for those argument types, GOOPS calls | |
1556 | the @code{no-applicable-method} generic function. The default method | |
1557 | calls @code{goops-error} with an appropriate message. | |
1558 | @end deffn | |
1559 | ||
1560 | @deffn generic no-next-method | |
1561 | @deffnx method no-next-method (gf <generic>) args | |
1562 | When a generic function method calls @code{(next-method)} to invoke the | |
1563 | next less specialized method for that generic function, and no less | |
1564 | specialized methods have been defined for the current generic function | |
1565 | arguments, GOOPS calls the @code{no-next-method} generic function. The | |
1566 | default method calls @code{goops-error} with an appropriate message. | |
1567 | @end deffn | |
1568 | ||
1569 | @node Redefining a Class | |
c34d74ff | 1570 | @section Redefining a Class |
a0e07ba4 NJ |
1571 | |
1572 | Suppose that a class @code{<my-class>} is defined using @code{define-class} | |
8445eb1d | 1573 | (@pxref{Defining New Classes,, define-class}), with slots that have |
a0e07ba4 NJ |
1574 | accessor functions, and that an application has created several instances |
1575 | of @code{<my-class>} using @code{make} (@pxref{Basic Instance Creation,, | |
1576 | make}). What then happens if @code{<my-class>} is redefined by calling | |
1577 | @code{define-class} again? | |
1578 | ||
1579 | @menu | |
1580 | * Default Class Redefinition Behaviour:: | |
1581 | * Customizing Class Redefinition:: | |
1582 | @end menu | |
1583 | ||
1584 | @node Default Class Redefinition Behaviour | |
c34d74ff | 1585 | @subsection Default Class Redefinition Behaviour |
a0e07ba4 NJ |
1586 | |
1587 | GOOPS' default answer to this question is as follows. | |
1588 | ||
1589 | @itemize @bullet | |
1590 | @item | |
1591 | All existing direct instances of @code{<my-class>} are converted to be | |
1592 | instances of the new class. This is achieved by preserving the values | |
1593 | of slots that exist in both the old and new definitions, and initializing the | |
1594 | values of new slots in the usual way (@pxref{Basic Instance Creation,, | |
1595 | make}). | |
1596 | ||
1597 | @item | |
1598 | All existing subclasses of @code{<my-class>} are redefined, as though | |
1599 | the @code{define-class} expressions that defined them were re-evaluated | |
1600 | following the redefinition of @code{<my-class>}, and the class | |
1601 | redefinition process described here is applied recursively to the | |
1602 | redefined subclasses. | |
1603 | ||
1604 | @item | |
1605 | Once all of its instances and subclasses have been updated, the class | |
1606 | metaobject previously bound to the variable @code{<my-class>} is no | |
1607 | longer needed and so can be allowed to be garbage collected. | |
1608 | @end itemize | |
1609 | ||
1610 | To keep things tidy, GOOPS also needs to do a little housekeeping on | |
1611 | methods that are associated with the redefined class. | |
1612 | ||
1613 | @itemize @bullet | |
1614 | @item | |
1615 | Slot accessor methods for slots in the old definition should be removed | |
1616 | from their generic functions. They will be replaced by accessor methods | |
1617 | for the slots of the new class definition. | |
1618 | ||
1619 | @item | |
1620 | Any generic function method that uses the old @code{<my-class>} metaobject | |
1621 | as one of its formal parameter specializers must be updated to refer to | |
1622 | the new @code{<my-class>} metaobject. (Whenever a new generic function | |
1623 | method is defined, @code{define-method} adds the method to a list stored | |
1624 | in the class metaobject for each class used as a formal parameter | |
1625 | specializer, so it is easy to identify all the methods that must be | |
1626 | updated when a class is redefined.) | |
1627 | @end itemize | |
1628 | ||
1629 | If this class redefinition strategy strikes you as rather counter-intuitive, | |
1630 | bear in mind that it is derived from similar behaviour in other object | |
1631 | systems such as CLOS, and that experience in those systems has shown it to be | |
1632 | very useful in practice. | |
1633 | ||
1634 | Also bear in mind that, like most of GOOPS' default behaviour, it can | |
1635 | be customized@dots{} | |
1636 | ||
1637 | @node Customizing Class Redefinition | |
c34d74ff | 1638 | @subsection Customizing Class Redefinition |
a0e07ba4 NJ |
1639 | |
1640 | When @code{define-class} notices that a class is being redefined, | |
1641 | it constructs the new class metaobject as usual, and then invokes the | |
1642 | @code{class-redefinition} generic function with the old and new classes | |
1643 | as arguments. Therefore, if the old or new classes have metaclasses | |
1644 | other than the default @code{<class>}, class redefinition behaviour can | |
1645 | be customized by defining a @code{class-redefinition} method that is | |
1646 | specialized for the relevant metaclasses. | |
1647 | ||
1648 | @deffn generic class-redefinition | |
1649 | Handle the class redefinition from @var{old-class} to @var{new-class}, | |
1650 | and return the new class metaobject that should be bound to the | |
1651 | variable specified by @code{define-class}'s first argument. | |
1652 | @end deffn | |
1653 | ||
1654 | @deffn method class-redefinition (old-class <class>) (new-class <class>) | |
1655 | Implements GOOPS' default class redefinition behaviour, as described in | |
1656 | @ref{Default Class Redefinition Behaviour}. Returns the metaobject | |
1657 | for the new class definition. | |
1658 | @end deffn | |
1659 | ||
1660 | An alternative class redefinition strategy could be to leave all | |
1661 | existing instances as instances of the old class, but accepting that the | |
1662 | old class is now ``nameless'', since its name has been taken over by the | |
1663 | new definition. In this strategy, any existing subclasses could also | |
1664 | be left as they are, on the understanding that they inherit from a nameless | |
1665 | superclass. | |
1666 | ||
1667 | This strategy is easily implemented in GOOPS, by defining a new metaclass, | |
1668 | that will be used as the metaclass for all classes to which the strategy | |
1669 | should apply, and then defining a @code{class-redefinition} method that | |
1670 | is specialized for this metaclass: | |
1671 | ||
1672 | @example | |
1673 | (define-class <can-be-nameless> (<class>)) | |
1674 | ||
45867c2a NJ |
1675 | (define-method (class-redefinition (old <can-be-nameless>) |
1676 | (new <class>)) | |
a0e07ba4 NJ |
1677 | new) |
1678 | @end example | |
1679 | ||
1680 | When customization can be as easy as this, aren't you glad that GOOPS | |
1681 | implements the far more difficult strategy as its default! | |
1682 | ||
1683 | Finally, note that, if @code{class-redefinition} itself is not customized, | |
1684 | the default @code{class-redefinition} method invokes three further | |
1685 | generic functions that could be individually customized: | |
1686 | ||
1687 | @itemize @bullet | |
1688 | @item | |
1689 | (remove-class-accessors! @var{old-class}) | |
1690 | ||
1691 | @item | |
1692 | (update-direct-method! @var{method} @var{old-class} @var{new-class}) | |
1693 | ||
1694 | @item | |
1695 | (update-direct-subclass! @var{subclass} @var{old-class} @var{new-class}) | |
1696 | @end itemize | |
1697 | ||
1698 | and the default methods for these generic functions invoke further | |
1699 | generic functions, and so on@dots{} The detailed protocol for all of these | |
1700 | is described in @ref{MOP Specification}. | |
1701 | ||
1702 | @node Changing the Class of an Instance | |
c34d74ff | 1703 | @section Changing the Class of an Instance |
a0e07ba4 NJ |
1704 | |
1705 | You can change the class of an existing instance by invoking the | |
1706 | generic function @code{change-class} with two arguments: the instance | |
1707 | and the new class. | |
1708 | ||
1709 | @deffn generic change-class | |
1710 | @end deffn | |
1711 | ||
1712 | The default method for @code{change-class} decides how to implement the | |
1713 | change of class by looking at the slot definitions for the instance's | |
1714 | existing class and for the new class. If the new class has slots with | |
1715 | the same name as slots in the existing class, the values for those slots | |
1716 | are preserved. Slots that are present only in the existing class are | |
1717 | discarded. Slots that are present only in the new class are initialized | |
1718 | using the corresponding slot definition's init function (@pxref{Classes,, | |
1719 | slot-init-function}). | |
1720 | ||
1721 | @deffn {method} change-class (obj <object>) (new <class>) | |
1722 | Modify instance @var{obj} to make it an instance of class @var{new}. | |
1723 | ||
1724 | The value of each of @var{obj}'s slots is preserved only if a similarly named | |
1725 | slot exists in @var{new}; any other slot values are discarded. | |
1726 | ||
1727 | The slots in @var{new} that do not correspond to any of @var{obj}'s | |
1728 | pre-existing slots are initialized according to @var{new}'s slot definitions' | |
1729 | init functions. | |
1730 | @end deffn | |
1731 | ||
1732 | Customized change of class behaviour can be implemented by defining | |
1733 | @code{change-class} methods that are specialized either by the class | |
1734 | of the instances to be modified or by the metaclass of the new class. | |
1735 | ||
1736 | When a class is redefined (@pxref{Redefining a Class}), and the default | |
1737 | class redefinition behaviour is not overridden, GOOPS (eventually) | |
1738 | invokes the @code{change-class} generic function for each existing | |
1739 | instance of the redefined class. | |
1740 | ||
1741 | @node Introspection | |
c34d74ff | 1742 | @section Introspection |
a0e07ba4 NJ |
1743 | |
1744 | @dfn{Introspection}, also known as @dfn{reflection}, is the name given | |
1745 | to the ability to obtain information dynamically about GOOPS metaobjects. | |
1746 | It is perhaps best illustrated by considering an object oriented language | |
1747 | that does not provide any introspection, namely C++. | |
1748 | ||
1749 | Nothing in C++ allows a running program to obtain answers to the following | |
1750 | types of question: | |
1751 | ||
1752 | @itemize @bullet | |
1753 | @item | |
1754 | What are the data members of this object or class? | |
1755 | ||
1756 | @item | |
1757 | What classes does this class inherit from? | |
1758 | ||
1759 | @item | |
1760 | Is this method call virtual or non-virtual? | |
1761 | ||
1762 | @item | |
1763 | If I invoke @code{Employee::adjustHoliday()}, what class contains the | |
1764 | @code{adjustHoliday()} method that will be applied? | |
1765 | @end itemize | |
1766 | ||
1767 | In C++, answers to such questions can only be determined by looking at | |
1768 | the source code, if you have access to it. GOOPS, on the other hand, | |
1769 | includes procedures that allow answers to these questions --- or their | |
1770 | GOOPS equivalents --- to be obtained dynamically, at run time. | |
1771 | ||
1772 | @menu | |
1773 | * Classes:: | |
1774 | * Slots:: | |
1775 | * Instances:: | |
1776 | * Generic Functions:: | |
1777 | * Generic Function Methods:: | |
1778 | @end menu | |
1779 | ||
1780 | @node Classes | |
c34d74ff | 1781 | @subsection Classes |
a0e07ba4 NJ |
1782 | |
1783 | @deffn {primitive procedure} class-name class | |
1784 | Return the name of class @var{class}. | |
1785 | This is the value of the @var{class} metaobject's @code{name} slot. | |
1786 | @end deffn | |
1787 | ||
1788 | @deffn {primitive procedure} class-direct-supers class | |
1789 | Return a list containing the direct superclasses of @var{class}. | |
1790 | This is the value of the @var{class} metaobject's | |
1791 | @code{direct-supers} slot. | |
1792 | @end deffn | |
1793 | ||
1794 | @deffn {primitive procedure} class-direct-slots class | |
1795 | Return a list containing the slot definitions of the direct slots of | |
1796 | @var{class}. | |
1797 | This is the value of the @var{class} metaobject's @code{direct-slots} | |
1798 | slot. | |
1799 | @end deffn | |
1800 | ||
1801 | @deffn {primitive procedure} class-direct-subclasses class | |
1802 | Return a list containing the direct subclasses of @var{class}. | |
1803 | This is the value of the @var{class} metaobject's | |
1804 | @code{direct-subclasses} slot. | |
1805 | @end deffn | |
1806 | ||
1807 | @deffn {primitive procedure} class-direct-methods class | |
1808 | Return a list of all the generic function methods that use @var{class} | |
1809 | as a formal parameter specializer. | |
1810 | This is the value of the @var{class} metaobject's @code{direct-methods} | |
1811 | slot. | |
1812 | @end deffn | |
1813 | ||
1814 | @deffn {primitive procedure} class-precedence-list class | |
1815 | Return the class precedence list for class @var{class} (@pxref{Class | |
1816 | precedence list}). | |
1817 | This is the value of the @var{class} metaobject's @code{cpl} slot. | |
1818 | @end deffn | |
1819 | ||
1820 | @deffn {primitive procedure} class-slots class | |
1821 | Return a list containing the slot definitions for all @var{class}'s slots, | |
1822 | including any slots that are inherited from superclasses. | |
1823 | This is the value of the @var{class} metaobject's @code{slots} slot. | |
1824 | @end deffn | |
1825 | ||
1826 | @deffn {primitive procedure} class-environment class | |
1827 | Return the value of @var{class}'s @code{environment} slot. | |
1828 | [ *fixme* I don't know what this value is used for. ] | |
1829 | @end deffn | |
1830 | ||
1831 | @deffn procedure class-subclasses class | |
1832 | Return a list of all subclasses of @var{class}. | |
1833 | @end deffn | |
1834 | ||
1835 | @deffn procedure class-methods class | |
1836 | Return a list of all methods that use @var{class} or a subclass of | |
1837 | @var{class} as one of its formal parameter specializers. | |
1838 | @end deffn | |
1839 | ||
1840 | @node Slots | |
c34d74ff | 1841 | @subsection Slots |
a0e07ba4 NJ |
1842 | |
1843 | @deffn procedure class-slot-definition class slot-name | |
1844 | Return the slot definition for the slot named @var{slot-name} in class | |
1845 | @var{class}. @var{slot-name} should be a symbol. | |
1846 | @end deffn | |
1847 | ||
1848 | @deffn procedure slot-definition-name slot-def | |
1849 | Extract and return the slot name from @var{slot-def}. | |
1850 | @end deffn | |
1851 | ||
1852 | @deffn procedure slot-definition-options slot-def | |
1853 | Extract and return the slot options from @var{slot-def}. | |
1854 | @end deffn | |
1855 | ||
1856 | @deffn procedure slot-definition-allocation slot-def | |
1857 | Extract and return the slot allocation option from @var{slot-def}. This | |
1858 | is the value of the @code{#:allocation} keyword (@pxref{Slot Options,, | |
1859 | allocation}), or @code{#:instance} if the @code{#:allocation} keyword is | |
1860 | absent. | |
1861 | @end deffn | |
1862 | ||
1863 | @deffn procedure slot-definition-getter slot-def | |
1864 | Extract and return the slot getter option from @var{slot-def}. This is | |
1865 | the value of the @code{#:getter} keyword (@pxref{Slot Options,, | |
1866 | getter}), or @code{#f} if the @code{#:getter} keyword is absent. | |
1867 | @end deffn | |
1868 | ||
1869 | @deffn procedure slot-definition-setter slot-def | |
1870 | Extract and return the slot setter option from @var{slot-def}. This is | |
1871 | the value of the @code{#:setter} keyword (@pxref{Slot Options,, | |
1872 | setter}), or @code{#f} if the @code{#:setter} keyword is absent. | |
1873 | @end deffn | |
1874 | ||
1875 | @deffn procedure slot-definition-accessor slot-def | |
1876 | Extract and return the slot accessor option from @var{slot-def}. This | |
1877 | is the value of the @code{#:accessor} keyword (@pxref{Slot Options,, | |
1878 | accessor}), or @code{#f} if the @code{#:accessor} keyword is absent. | |
1879 | @end deffn | |
1880 | ||
1881 | @deffn procedure slot-definition-init-value slot-def | |
1882 | Extract and return the slot init-value option from @var{slot-def}. This | |
1883 | is the value of the @code{#:init-value} keyword (@pxref{Slot Options,, | |
1884 | init-value}), or the unbound value if the @code{#:init-value} keyword is | |
1885 | absent. | |
1886 | @end deffn | |
1887 | ||
1888 | @deffn procedure slot-definition-init-form slot-def | |
1889 | Extract and return the slot init-form option from @var{slot-def}. This | |
1890 | is the value of the @code{#:init-form} keyword (@pxref{Slot Options,, | |
1891 | init-form}), or the unbound value if the @code{#:init-form} keyword is | |
1892 | absent. | |
1893 | @end deffn | |
1894 | ||
1895 | @deffn procedure slot-definition-init-thunk slot-def | |
1896 | Extract and return the slot init-thunk option from @var{slot-def}. This | |
1897 | is the value of the @code{#:init-thunk} keyword (@pxref{Slot Options,, | |
1898 | init-thunk}), or @code{#f} if the @code{#:init-thunk} keyword is absent. | |
1899 | @end deffn | |
1900 | ||
1901 | @deffn procedure slot-definition-init-keyword slot-def | |
1902 | Extract and return the slot init-keyword option from @var{slot-def}. | |
1903 | This is the value of the @code{#:init-keyword} keyword (@pxref{Slot | |
1904 | Options,, init-keyword}), or @code{#f} if the @code{#:init-keyword} | |
1905 | keyword is absent. | |
1906 | @end deffn | |
1907 | ||
1908 | @deffn procedure slot-init-function class slot-name | |
1909 | Return the initialization function for the slot named @var{slot-name} in | |
1910 | class @var{class}. @var{slot-name} should be a symbol. | |
1911 | ||
1912 | The returned initialization function incorporates the effects of the | |
1913 | standard @code{#:init-thunk}, @code{#:init-form} and @code{#:init-value} | |
1914 | slot options. These initializations can be overridden by the | |
1915 | @code{#:init-keyword} slot option or by a specialized @code{initialize} | |
1916 | method, so, in general, the function returned by | |
1917 | @code{slot-init-function} may be irrelevant. For a fuller discussion, | |
1918 | see @ref{Slot Options,, init-value}. | |
1919 | @end deffn | |
1920 | ||
1921 | @node Instances | |
c34d74ff | 1922 | @subsection Instances |
a0e07ba4 NJ |
1923 | |
1924 | @deffn {primitive procedure} class-of value | |
1925 | Return the GOOPS class of any Scheme @var{value}. | |
1926 | @end deffn | |
1927 | ||
1928 | @deffn {primitive procedure} instance? object | |
1929 | Return @code{#t} if @var{object} is any GOOPS instance, otherwise | |
1930 | @code{#f}. | |
1931 | @end deffn | |
1932 | ||
1933 | @deffn procedure is-a? object class | |
1934 | Return @code{#t} if @var{object} is an instance of @var{class} or one of | |
1935 | its subclasses. | |
1936 | @end deffn | |
1937 | ||
1938 | Implementation notes: @code{is-a?} uses @code{class-of} and | |
1939 | @code{class-precedence-list} to obtain the class precedence list for | |
1940 | @var{object}. | |
1941 | ||
1942 | @node Generic Functions | |
c34d74ff | 1943 | @subsection Generic Functions |
a0e07ba4 NJ |
1944 | |
1945 | @deffn {primitive procedure} generic-function-name gf | |
1946 | Return the name of generic function @var{gf}. | |
1947 | @end deffn | |
1948 | ||
1949 | @deffn {primitive procedure} generic-function-methods gf | |
1950 | Return a list of the methods of generic function @var{gf}. | |
1951 | This is the value of the @var{gf} metaobject's @code{methods} slot. | |
1952 | @end deffn | |
1953 | ||
1954 | @node Generic Function Methods | |
c34d74ff | 1955 | @subsection Generic Function Methods |
a0e07ba4 NJ |
1956 | |
1957 | @deffn {primitive procedure} method-generic-function method | |
1958 | Return the generic function that @var{method} belongs to. | |
1959 | This is the value of the @var{method} metaobject's | |
1960 | @code{generic-function} slot. | |
1961 | @end deffn | |
1962 | ||
1963 | @deffn {primitive procedure} method-specializers method | |
1964 | Return a list of @var{method}'s formal parameter specializers . | |
1965 | This is the value of the @var{method} metaobject's | |
1966 | @code{specializers} slot. | |
1967 | @end deffn | |
1968 | ||
1969 | @deffn {primitive procedure} method-procedure method | |
1970 | Return the procedure that implements @var{method}. | |
1971 | This is the value of the @var{method} metaobject's | |
1972 | @code{procedure} slot. | |
1973 | @end deffn | |
1974 | ||
1975 | @deffn generic method-source | |
1976 | @deffnx method method-source (m <method>) | |
1977 | Return an expression that prints to show the definition of method | |
1978 | @var{m}. | |
1979 | ||
1980 | @example | |
1981 | (define-generic cube) | |
1982 | ||
1983 | (define-method (cube (n <number>)) | |
1984 | (* n n n)) | |
1985 | ||
1986 | (map method-source (generic-function-methods cube)) | |
1987 | @result{} | |
1988 | ((method ((n <number>)) (* n n n))) | |
1989 | @end example | |
1990 | @end deffn | |
1991 | ||
1992 | @node Miscellaneous Functions | |
c34d74ff | 1993 | @section Miscellaneous Functions |
a0e07ba4 NJ |
1994 | |
1995 | @menu | |
1996 | * Administrative Functions:: | |
eb12b401 | 1997 | * GOOPS Error Handling:: |
a0e07ba4 NJ |
1998 | * Object Comparisons:: |
1999 | * Cloning Objects:: | |
2000 | * Write and Display:: | |
2001 | @end menu | |
2002 | ||
2003 | @node Administrative Functions | |
c34d74ff | 2004 | @subsection Administration Functions |
a0e07ba4 NJ |
2005 | |
2006 | This section describes administrative, non-technical GOOPS functions. | |
2007 | ||
2008 | @deffn primitive goops-version | |
2009 | Return the current GOOPS version as a string, for example ``0.2''. | |
2010 | @end deffn | |
2011 | ||
eb12b401 | 2012 | @node GOOPS Error Handling |
c34d74ff | 2013 | @subsection Error Handling |
a0e07ba4 NJ |
2014 | |
2015 | The procedure @code{goops-error} is called to raise an appropriate error | |
2016 | by the default methods of the following generic functions: | |
2017 | ||
2018 | @itemize @bullet | |
2019 | @item | |
2020 | @code{slot-missing} (@pxref{Handling Slot Access Errors,, slot-missing}) | |
2021 | ||
2022 | @item | |
2023 | @code{slot-unbound} (@pxref{Handling Slot Access Errors,, slot-unbound}) | |
2024 | ||
2025 | @item | |
2026 | @code{no-method} (@pxref{Handling Invocation Errors,, no-method}) | |
2027 | ||
2028 | @item | |
2029 | @code{no-applicable-method} (@pxref{Handling Invocation Errors,, | |
2030 | no-applicable-method}) | |
2031 | ||
2032 | @item | |
2033 | @code{no-next-method} (@pxref{Handling Invocation Errors,, | |
2034 | no-next-method}) | |
2035 | @end itemize | |
2036 | ||
2037 | If you customize these functions for particular classes or metaclasses, | |
2038 | you may still want to use @code{goops-error} to signal any error | |
2039 | conditions that you detect. | |
2040 | ||
2041 | @deffn procedure goops-error format-string . args | |
2042 | Raise an error with key @code{goops-error} and error message constructed | |
2043 | from @var{format-string} and @var{args}. Error message formatting is | |
2044 | as done by @code{scm-error}. | |
2045 | @end deffn | |
2046 | ||
2047 | @node Object Comparisons | |
c34d74ff | 2048 | @subsection Object Comparisons |
a0e07ba4 | 2049 | |
b3a9e3d5 MD |
2050 | @deffn generic eqv? |
2051 | @deffnx method eqv? ((x <top>) (y <top>)) | |
2052 | @deffnx generic equal? | |
2053 | @deffnx method equal? ((x <top>) (y <top>)) | |
2054 | @deffnx generic = | |
2055 | @deffnx method = ((x <number>) (y <number>)) | |
a0e07ba4 NJ |
2056 | Generic functions and default (unspecialized) methods for comparing two |
2057 | GOOPS objects. | |
2058 | ||
b3a9e3d5 MD |
2059 | The default method for @code{eqv?} returns @code{#t} for all values |
2060 | that are equal in the sense defined by R5RS and the Guile reference | |
2061 | manual, otherwise @code{#f}. The default method for @code{equal?} | |
2062 | returns @code{#t} or @code{#f} in the sense defined by R5RS and the | |
2063 | Guile reference manual. If no such comparison is defined, | |
2064 | @code{equal?} returns the result of a call to @code{eqv?}. The | |
2065 | default method for = returns @code{#t} if @var{x} and @var{y} are | |
2066 | numerically equal, otherwise @code{#f}. | |
2067 | ||
2068 | Application class authors may wish to define specialized methods for | |
2069 | @code{eqv?}, @code{equal?} and @code{=} that compare instances of the | |
2070 | same class for equality in whatever sense is useful to the | |
2071 | application. Such methods will only be called if the arguments have | |
2072 | the same class and the result of the comparison isn't defined by R5RS | |
2073 | and the Guile reference manual. | |
a0e07ba4 NJ |
2074 | @end deffn |
2075 | ||
2076 | @node Cloning Objects | |
c34d74ff | 2077 | @subsection Cloning Objects |
a0e07ba4 NJ |
2078 | |
2079 | @deffn generic shallow-clone | |
2080 | @deffnx method shallow-clone (self <object>) | |
2081 | Return a ``shallow'' clone of @var{self}. The default method makes a | |
2082 | shallow clone by allocating a new instance and copying slot values from | |
2083 | self to the new instance. Each slot value is copied either as an | |
2084 | immediate value or by reference. | |
2085 | @end deffn | |
2086 | ||
2087 | @deffn generic deep-clone | |
2088 | @deffnx method deep-clone (self <object>) | |
2089 | Return a ``deep'' clone of @var{self}. The default method makes a deep | |
2090 | clone by allocating a new instance and copying or cloning slot values | |
2091 | from self to the new instance. If a slot value is an instance | |
2092 | (satisfies @code{instance?}), it is cloned by calling @code{deep-clone} | |
2093 | on that value. Other slot values are copied either as immediate values | |
2094 | or by reference. | |
2095 | @end deffn | |
2096 | ||
2097 | @node Write and Display | |
c34d74ff | 2098 | @subsection Write and Display |
a0e07ba4 NJ |
2099 | |
2100 | @deffn {primitive generic} write object port | |
2101 | @deffnx {primitive generic} display object port | |
2102 | When GOOPS is loaded, @code{write} and @code{display} become generic | |
2103 | functions with special methods for printing | |
2104 | ||
2105 | @itemize @bullet | |
2106 | @item | |
2107 | objects - instances of the class @code{<object>} | |
2108 | ||
2109 | @item | |
2110 | foreign objects - instances of the class @code{<foreign-object>} | |
2111 | ||
2112 | @item | |
2113 | classes - instances of the class @code{<class>} | |
2114 | ||
2115 | @item | |
2116 | generic functions - instances of the class @code{<generic>} | |
2117 | ||
2118 | @item | |
2119 | methods - instances of the class @code{<method>}. | |
2120 | @end itemize | |
2121 | ||
2122 | @code{write} and @code{display} print non-GOOPS values in the same way | |
2123 | as the Guile primitive @code{write} and @code{display} functions. | |
2124 | @end deffn | |
2125 | ||
a9bf12c2 NJ |
2126 | @node The Metaobject Protocol |
2127 | @section The Metaobject Protocol | |
a0e07ba4 | 2128 | |
5aaf0709 NJ |
2129 | GOOPS is based on a ``metaobject protocol'' (aka ``MOP'') derived from |
2130 | the ones used in CLOS (the Common Lisp Object System), tiny-clos (a | |
2131 | small Scheme implementation of a subset of CLOS functionality) and | |
2132 | STKlos. | |
a0e07ba4 | 2133 | |
a9bf12c2 | 2134 | GOOPS can be used by application authors at a basic level without any |
5aaf0709 NJ |
2135 | need to understand what the MOP is and how it works. On the other hand, |
2136 | the MOP underlies even very simple customizations --- such as defining | |
2137 | an @code{initialize} method to customize the initialization of instances | |
2138 | of an application-defined class --- and an understanding of the MOP | |
2139 | makes it much easier to explain such customizations in a precise way. | |
2140 | And in the long run, understanding the MOP is the key both to | |
a9bf12c2 NJ |
2141 | understanding GOOPS at a deeper level and to taking full advantage of |
2142 | GOOPS' power, by customizing the behaviour of GOOPS itself. | |
a0e07ba4 | 2143 | |
a9bf12c2 NJ |
2144 | @menu |
2145 | * Metaobjects and the Metaobject Protocol:: | |
2146 | * Terminology:: | |
2147 | * MOP Specification:: | |
2148 | * Class Definition:: | |
2149 | * Instance Creation:: | |
2150 | * Class Redefinition:: | |
2151 | * Method Definition:: | |
2152 | * Generic Function Invocation:: | |
2153 | @end menu | |
2154 | ||
2155 | @node Metaobjects and the Metaobject Protocol | |
2156 | @subsection Metaobjects and the Metaobject Protocol | |
2157 | ||
5aaf0709 NJ |
2158 | The building blocks of GOOPS are classes, slot definitions, instances, |
2159 | generic functions and methods. A class is a grouping of inheritance | |
2160 | relations and slot definitions. An instance is an object with slots | |
2161 | that are allocated following the rules implied by its class's | |
a9bf12c2 NJ |
2162 | superclasses and slot definitions. A generic function is a collection |
2163 | of methods and rules for determining which of those methods to apply | |
2164 | when the generic function is invoked. A method is a procedure and a set | |
2165 | of specializers that specify the type of arguments to which the | |
2166 | procedure is applicable. | |
2167 | ||
2168 | Of these entities, GOOPS represents classes, generic functions and | |
2169 | methods as ``metaobjects''. In other words, the values in a GOOPS | |
2170 | program that describe classes, generic functions and methods, are | |
2171 | themselves instances (or ``objects'') of special GOOPS classes that | |
2172 | encapsulate the behaviour, respectively, of classes, generic functions, | |
2173 | and methods. | |
2174 | ||
2175 | (The other two entities are slot definitions and instances. Slot | |
2176 | definitions are not strictly instances, but every slot definition is | |
2177 | associated with a GOOPS class that specifies the behaviour of the slot | |
2178 | as regards accessibility and protection from garbage collection. | |
2179 | Instances are of course objects in the usual sense, and there is no | |
2180 | benefit from thinking of them as metaobjects.) | |
2181 | ||
2182 | The ``metaobject protocol'' (aka ``MOP'') is the specification of the | |
2183 | generic functions which determine the behaviour of these metaobjects and | |
2184 | the circumstances in which these generic functions are invoked. | |
2185 | ||
2186 | For a concrete example of what this means, consider how GOOPS calculates | |
2187 | the set of slots for a class that is being defined using | |
2188 | @code{define-class}. The desired set of slots is the union of the new | |
2189 | class's direct slots and the slots of all its superclasses. But | |
2190 | @code{define-class} itself does not perform this calculation. Instead, | |
2191 | there is a method of the @code{initialize} generic function that is | |
2192 | specialized for instances of type @code{<class>}, and it is this method | |
2193 | that performs the slot calculation. | |
2194 | ||
2195 | @code{initialize} is a generic function which GOOPS calls whenever a new | |
2196 | instance is created, immediately after allocating memory for a new | |
2197 | instance, in order to initialize the new instance's slots. The sequence | |
2198 | of steps is as follows. | |
2199 | ||
2200 | @itemize @bullet | |
2201 | @item | |
2202 | @code{define-class} uses @code{make} to make a new instance of the | |
5aaf0709 NJ |
2203 | @code{<class>} class, passing as initialization arguments the |
2204 | superclasses, slot definitions and class options that were specified in | |
2205 | the @code{define-class} form. | |
a9bf12c2 NJ |
2206 | |
2207 | @item | |
2208 | @code{make} allocates memory for the new instance, and then invokes the | |
2209 | @code{initialize} generic function to initialize the new instance's | |
2210 | slots. | |
2211 | ||
2212 | @item | |
2213 | The @code{initialize} generic function applies the method that is | |
2214 | specialized for instances of type @code{<class>}, and this method | |
2215 | performs the slot calculation. | |
2216 | @end itemize | |
2217 | ||
2218 | In other words, rather than being hardcoded in @code{define-class}, the | |
2219 | behaviour of class definition is encapsulated by generic function | |
2220 | methods that are specialized for the class @code{<class>}. | |
2221 | ||
2222 | It is possible to create a new class that inherits from @code{<class>}, | |
2223 | which is called a ``metaclass'', and to write a new @code{initialize} | |
2224 | method that is specialized for instances of the new metaclass. Then, if | |
2225 | the @code{define-class} form includes a @code{#:metaclass} class option | |
2226 | whose value is the new metaclass, the class that is defined by the | |
2227 | @code{define-class} form will be an instance of the new metaclass rather | |
2228 | than of the default @code{<class>}, and will be defined in accordance | |
2229 | with the new @code{initialize} method. Thus the default slot | |
2230 | calculation, as well as any other aspect of the new class's relationship | |
2231 | with its superclasses, can be modified or overridden. | |
2232 | ||
2233 | In a similar way, the behaviour of generic functions can be modified or | |
2234 | overridden by creating a new class that inherits from the standard | |
2235 | generic function class @code{<generic>}, writing appropriate methods | |
2236 | that are specialized to the new class, and creating new generic | |
2237 | functions that are instances of the new class. | |
2238 | ||
2239 | The same is true for method metaobjects. And the same basic mechanism | |
2240 | allows the application class author to write an @code{initialize} method | |
2241 | that is specialized to their application class, to initialize instances | |
2242 | of that class. | |
2243 | ||
2244 | Such is the power of the MOP. Note that @code{initialize} is just one | |
2245 | of a large number of generic functions that can be customized to modify | |
2246 | the behaviour of application objects and classes and of GOOPS itself. | |
5aaf0709 NJ |
2247 | Each following section covers a particular area of GOOPS functionality, |
2248 | and describes the generic functions that are relevant for customization | |
2249 | of that area. | |
2250 | ||
a9bf12c2 NJ |
2251 | @node Terminology |
2252 | @subsection Terminology | |
2253 | ||
2254 | It is assumed that the reader is already familiar with standard object | |
2255 | orientation concepts such as classes, objects/instances, | |
2256 | inheritance/subclassing, generic functions and methods, encapsulation | |
2257 | and polymorphism. | |
2258 | ||
2259 | This section explains some of the less well known concepts and | |
2260 | terminology that GOOPS uses, which are assumed by the following sections | |
2261 | of the reference manual. | |
2262 | ||
2263 | @subsubheading Metaclass | |
2264 | ||
2265 | A @dfn{metaclass} is the class of an object which represents a GOOPS | |
2266 | class. Put more succinctly, a metaclass is a class's class. | |
2267 | ||
2268 | Most GOOPS classes have the metaclass @code{<class>} and, by default, | |
2269 | any new class that is created using @code{define-class} has the | |
2270 | metaclass @code{<class>}. | |
2271 | ||
2272 | But what does this really mean? To find out, let's look in more detail | |
2273 | at what happens when a new class is created using @code{define-class}: | |
2274 | ||
2275 | @example | |
2276 | (define-class <my-class> (<object>) . slots) | |
2277 | @end example | |
2278 | ||
2279 | GOOPS actually expands the @code{define-class} form to something like | |
2280 | this | |
2281 | ||
2282 | @example | |
2283 | (define <my-class> (class (<object>) . slots)) | |
2284 | @end example | |
2285 | ||
2286 | and thence to | |
2287 | ||
2288 | @example | |
2289 | (define <my-class> | |
2290 | (make <class> #:supers (list <object>) #:slots slots)) | |
2291 | @end example | |
2292 | ||
2293 | In other words, the value of @code{<my-class>} is in fact an instance of | |
2294 | the class @code{<class>} with slot values specifying the superclasses | |
2295 | and slot definitions for the class @code{<my-class>}. (@code{#:supers} | |
2296 | and @code{#:slots} are initialization keywords for the @code{dsupers} | |
2297 | and @code{dslots} slots of the @code{<class>} class.) | |
2298 | ||
2299 | In order to take advantage of the full power of the GOOPS metaobject | |
2300 | protocol (@pxref{MOP Specification}), it is sometimes desirable to | |
2301 | create a new class with a metaclass other than the default | |
2302 | @code{<class>}. This is done by writing: | |
2303 | ||
2304 | @example | |
2305 | (define-class <my-class2> (<object>) | |
2306 | slot @dots{} | |
2307 | #:metaclass <my-metaclass>) | |
2308 | @end example | |
2309 | ||
2310 | GOOPS expands this to something like: | |
2311 | ||
2312 | @example | |
2313 | (define <my-class2> | |
2314 | (make <my-metaclass> #:supers (list <object>) #:slots slots)) | |
2315 | @end example | |
2316 | ||
2317 | In this case, the value of @code{<my-class2>} is an instance of the more | |
2318 | specialized class @code{<my-metaclass>}. Note that | |
2319 | @code{<my-metaclass>} itself must previously have been defined as a | |
2320 | subclass of @code{<class>}. For a full discussion of when and how it is | |
2321 | useful to define new metaclasses, see @ref{MOP Specification}. | |
2322 | ||
2323 | Now let's make an instance of @code{<my-class2>}: | |
2324 | ||
2325 | @example | |
2326 | (define my-object (make <my-class2> ...)) | |
2327 | @end example | |
2328 | ||
2329 | All of the following statements are correct expressions of the | |
2330 | relationships between @code{my-object}, @code{<my-class2>}, | |
2331 | @code{<my-metaclass>} and @code{<class>}. | |
2332 | ||
2333 | @itemize @bullet | |
2334 | @item | |
2335 | @code{my-object} is an instance of the class @code{<my-class2>}. | |
2336 | ||
2337 | @item | |
2338 | @code{<my-class2>} is an instance of the class @code{<my-metaclass>}. | |
2339 | ||
2340 | @item | |
2341 | @code{<my-metaclass>} is an instance of the class @code{<class>}. | |
2342 | ||
2343 | @item | |
2344 | The class of @code{my-object} is @code{<my-class2>}. | |
2345 | ||
2346 | @item | |
2347 | The metaclass of @code{my-object} is @code{<my-metaclass>}. | |
2348 | ||
2349 | @item | |
2350 | The class of @code{<my-class2>} is @code{<my-metaclass>}. | |
2351 | ||
2352 | @item | |
2353 | The metaclass of @code{<my-class2>} is @code{<class>}. | |
2354 | ||
2355 | @item | |
2356 | The class of @code{<my-metaclass>} is @code{<class>}. | |
2357 | ||
2358 | @item | |
2359 | The metaclass of @code{<my-metaclass>} is @code{<class>}. | |
2360 | ||
2361 | @item | |
2362 | @code{<my-class2>} is not a metaclass, since it is does not inherit from | |
2363 | @code{<class>}. | |
2364 | ||
2365 | @item | |
2366 | @code{<my-metaclass>} is a metaclass, since it inherits from | |
2367 | @code{<class>}. | |
2368 | @end itemize | |
2369 | ||
2370 | @subsubheading Class Precedence List | |
2371 | ||
2372 | The @dfn{class precedence list} of a class is the list of all direct and | |
2373 | indirect superclasses of that class, including the class itself. | |
2374 | ||
2375 | In the absence of multiple inheritance, the class precedence list is | |
2376 | ordered straightforwardly, beginning with the class itself and ending | |
2377 | with @code{<top>}. | |
2378 | ||
2379 | For example, given this inheritance hierarchy: | |
2380 | ||
2381 | @example | |
2382 | (define-class <invertebrate> (<object>) @dots{}) | |
2383 | (define-class <echinoderm> (<invertebrate>) @dots{}) | |
2384 | (define-class <starfish> (<echinoderm>) @dots{}) | |
2385 | @end example | |
2386 | ||
2387 | the class precedence list of <starfish> would be | |
2388 | ||
2389 | @example | |
2390 | (<starfish> <echinoderm> <invertebrate> <object> <top>) | |
2391 | @end example | |
2392 | ||
2393 | With multiple inheritance, the algorithm is a little more complicated. | |
2394 | A full description is provided by the GOOPS Tutorial: see @ref{Class | |
2395 | precedence list}. | |
2396 | ||
2397 | ``Class precedence list'' is often abbreviated, in documentation and | |
2398 | Scheme variable names, to @dfn{cpl}. | |
2399 | ||
2400 | @subsubheading Accessor | |
2401 | ||
2402 | An @dfn{accessor} is a generic function with both reference and setter | |
2403 | methods. | |
2404 | ||
2405 | @example | |
2406 | (define-accessor perimeter) | |
2407 | @end example | |
2408 | ||
2409 | Reference methods for an accessor are defined in the same way as generic | |
2410 | function methods. | |
2411 | ||
2412 | @example | |
2413 | (define-method (perimeter (s <square>)) | |
2414 | (* 4 (side-length s))) | |
2415 | @end example | |
2416 | ||
2417 | Setter methods for an accessor are defined by specifying ``(setter | |
2418 | <accessor-name>)'' as the first parameter of the @code{define-method} | |
2419 | call. | |
2420 | ||
2421 | @example | |
2422 | (define-method ((setter perimeter) (s <square>) (n <number>)) | |
2423 | (set! (side-length s) (/ n 4))) | |
2424 | @end example | |
2425 | ||
2426 | Once an appropriate setter method has been defined in this way, it can | |
2427 | be invoked using the generalized @code{set!} syntax, as in: | |
2428 | ||
2429 | @example | |
2430 | (set! (perimeter s1) 18.3) | |
2431 | @end example | |
2432 | ||
2433 | @node MOP Specification | |
2434 | @subsection MOP Specification | |
2435 | ||
2436 | The aim of the MOP specification in this chapter is to specify all the | |
2437 | customizable generic function invocations that can be made by the standard | |
2438 | GOOPS syntax, procedures and methods, and to explain the protocol for | |
2439 | customizing such invocations. | |
2440 | ||
2441 | A generic function invocation is customizable if the types of the arguments | |
2442 | to which it is applied are not all determined by the lexical context in | |
2443 | which the invocation appears. For example, | |
2444 | ||
2445 | @itemize @bullet | |
2446 | @item | |
2447 | the @code{(initialize @var{instance} @var{initargs})} invocation in the | |
2448 | default @code{make-instance} method is customizable, because the type of the | |
2449 | @code{@var{instance}} argument is determined by the class that was passed to | |
2450 | @code{make-instance}. | |
a0e07ba4 NJ |
2451 | |
2452 | @item | |
2453 | the @code{(make <generic> #:name ',name)} invocation in @code{define-generic} | |
2454 | is not customizable, because all of its arguments have lexically determined | |
2455 | types. | |
2456 | @end itemize | |
2457 | ||
2458 | When using this rule to decide whether a given generic function invocation | |
2459 | is customizable, we ignore arguments that are expected to be handled in | |
2460 | method definitions as a single ``rest'' list argument. | |
2461 | ||
2462 | For each customizable generic function invocation, the @dfn{invocation | |
2463 | protocol} is explained by specifying | |
2464 | ||
2465 | @itemize @bullet | |
2466 | @item | |
2467 | what, conceptually, the applied method is intended to do | |
2468 | ||
2469 | @item | |
2470 | what assumptions, if any, the caller makes about the applied method's side | |
2471 | effects | |
2472 | ||
2473 | @item | |
2474 | what the caller expects to get as the applied method's return value. | |
2475 | @end itemize | |
2476 | ||
a0e07ba4 | 2477 | @node Class Definition |
eb12b401 | 2478 | @subsection Class Definition |
a0e07ba4 NJ |
2479 | |
2480 | @code{define-class} (syntax) | |
2481 | ||
2482 | @itemize @bullet | |
2483 | @item | |
2484 | @code{class} (syntax) | |
2485 | ||
2486 | @itemize @bullet | |
2487 | @item | |
2488 | @code{make-class} (procedure) | |
2489 | ||
2490 | @itemize @bullet | |
2491 | @item | |
2492 | @code{make @var{metaclass} @dots{}} (generic) | |
2493 | ||
2494 | @var{metaclass} is the metaclass of the class being defined, either | |
2495 | taken from the @code{#:metaclass} class option or computed by | |
2496 | @code{ensure-metaclass}. The applied method must create and return the | |
2497 | fully initialized class metaobject for the new class definition. | |
2498 | @end itemize | |
2499 | ||
2500 | @end itemize | |
2501 | ||
2502 | @item | |
2503 | @code{class-redefinition @var{old-class} @var{new-class}} (generic) | |
2504 | ||
2505 | @code{define-class} calls @code{class-redefinition} if the variable | |
2506 | specified by its first argument already held a GOOPS class definition. | |
2507 | @var{old-class} and @var{new-class} are the old and new class metaobjects. | |
2508 | The applied method should perform whatever is necessary to handle the | |
2509 | redefinition, and should return the class metaobject that is to be bound | |
2510 | to @code{define-class}'s variable. The default class redefinition | |
2511 | protocol is described in @ref{Class Redefinition}. | |
2512 | @end itemize | |
2513 | ||
2514 | The @code{(make @var{metaclass} @dots{})} invocation above will create | |
2515 | an class metaobject with metaclass @var{metaclass}. By default, this | |
2516 | metaobject will be initialized by the @code{initialize} method that is | |
2517 | specialized for instances of type @code{<class>}. | |
2518 | ||
2519 | @code{initialize <class> @var{initargs}} (method) | |
2520 | ||
2521 | @itemize @bullet | |
2522 | @item | |
2523 | @code{compute-cpl @var{class}} (generic) | |
2524 | ||
2525 | The applied method should compute and return the class precedence list | |
2526 | for @var{class} as a list of class metaobjects. When @code{compute-cpl} | |
2527 | is called, the following @var{class} metaobject slots have all been | |
2528 | initialized: @code{name}, @code{direct-supers}, @code{direct-slots}, | |
2529 | @code{direct-subclasses} (empty), @code{direct-methods}. The value | |
2530 | returned by @code{compute-cpl} will be stored in the @code{cpl} slot. | |
2531 | ||
2532 | @item | |
2533 | @code{compute-slots @var{class}} (generic) | |
2534 | ||
2535 | The applied method should compute and return the slots (union of direct | |
2536 | and inherited) for @var{class} as a list of slot definitions. When | |
2537 | @code{compute-slots} is called, all the @var{class} metaobject slots | |
2538 | mentioned for @code{compute-cpl} have been initialized, plus the | |
2539 | following: @code{cpl}, @code{redefined} (@code{#f}), @code{environment}. | |
2540 | The value returned by @code{compute-slots} will be stored in the | |
2541 | @code{slots} slot. | |
2542 | ||
2543 | @item | |
2544 | @code{compute-get-n-set @var{class} @var{slot-def}} (generic) | |
2545 | ||
2546 | @code{initialize} calls @code{compute-get-n-set} for each slot computed | |
2547 | by @code{compute-slots}. The applied method should compute and return a | |
2548 | pair of closures that, respectively, get and set the value of the specified | |
2549 | slot. The get closure should have arity 1 and expect a single argument | |
2550 | that is the instance whose slot value is to be retrieved. The set closure | |
2551 | should have arity 2 and expect two arguments, where the first argument is | |
2552 | the instance whose slot value is to be set and the second argument is the | |
2553 | new value for that slot. The closures should be returned in a two element | |
2554 | list: @code{(list @var{get} @var{set})}. | |
2555 | ||
2556 | The closures returned by @code{compute-get-n-set} are stored as part of | |
2557 | the value of the @var{class} metaobject's @code{getters-n-setters} slot. | |
2558 | Specifically, the value of this slot is a list with the same number of | |
2559 | elements as there are slots in the class, and each element looks either like | |
2560 | ||
2561 | @example | |
2562 | @code{(@var{slot-name-symbol} @var{init-function} . @var{index})} | |
2563 | @end example | |
2564 | ||
2565 | or like | |
2566 | ||
2567 | @example | |
2568 | @code{(@var{slot-name-symbol} @var{init-function} @var{get} @var{set})} | |
2569 | @end example | |
2570 | ||
2571 | Where the get and set closures are replaced by @var{index}, the slot is | |
2572 | an instance slot and @var{index} is the slot's index in the underlying | |
2573 | structure: GOOPS knows how to get and set the value of such slots and so | |
2574 | does not need specially constructed get and set closures. Otherwise, | |
2575 | @var{get} and @var{set} are the closures returned by @code{compute-get-n-set}. | |
2576 | ||
2577 | The structure of the @code{getters-n-setters} slot value is important when | |
2578 | understanding the next customizable generic functions that @code{initialize} | |
2579 | calls@dots{} | |
2580 | ||
2581 | @item | |
2582 | @code{compute-getter-method @var{class} @var{gns}} (generic) | |
2583 | ||
2584 | @code{initialize} calls @code{compute-getter-method} for each of the class's | |
2585 | slots (as determined by @code{compute-slots}) that includes a | |
2586 | @code{#:getter} or @code{#:accessor} slot option. @var{gns} is the | |
2587 | element of the @var{class} metaobject's @code{getters-n-setters} slot that | |
2588 | specifies how the slot in question is referenced and set, as described | |
2589 | above under @code{compute-get-n-set}. The applied method should create | |
2590 | and return a method that is specialized for instances of type @var{class} | |
2591 | and uses the get closure to retrieve the slot's value. [ *fixme Need | |
2592 | to insert something here about checking that the value is not unbound. ] | |
2593 | @code{initialize} uses @code{add-method!} to add the returned method to | |
2594 | the generic function named by the slot definition's @code{#:getter} or | |
2595 | @code{#:accessor} option. | |
2596 | ||
2597 | @item | |
2598 | @code{compute-setter-method @var{class} @var{gns}} (generic) | |
2599 | ||
2600 | @code{compute-setter-method} is invoked with the same arguments as | |
2601 | @code{compute-getter-method}, for each of the class's slots that includes | |
2602 | a @code{#:setter} or @code{#:accessor} slot option. The applied method | |
2603 | should create and return a method that is specialized for instances of | |
2604 | type @var{class} and uses the set closure to set the slot's value. | |
2605 | @code{initialize} then uses @code{add-method!} to add the returned method | |
2606 | to the generic function named by the slot definition's @code{#:setter} | |
2607 | or @code{#:accessor} option. | |
2608 | @end itemize | |
2609 | ||
2610 | @node Instance Creation | |
eb12b401 | 2611 | @subsection Instance Creation |
a0e07ba4 NJ |
2612 | |
2613 | @code{make <class> . @var{initargs}} (method) | |
2614 | ||
2615 | @itemize @bullet | |
2616 | @item | |
2617 | @code{allocate-instance @var{class} @var{initargs}} (generic) | |
2618 | ||
2619 | The applied @code{allocate-instance} method should allocate storage for | |
2620 | a new instance of class @var{class} and return the uninitialized instance. | |
2621 | ||
2622 | @item | |
2623 | @code{initialize @var{instance} @var{initargs}} (generic) | |
2624 | ||
2625 | @var{instance} is the uninitialized instance returned by | |
2626 | @code{allocate-instance}. The applied method should initialize the new | |
2627 | instance in whatever sense is appropriate for its class. The method's | |
2628 | return value is ignored. | |
2629 | @end itemize | |
2630 | ||
2631 | @node Class Redefinition | |
eb12b401 | 2632 | @subsection Class Redefinition |
a0e07ba4 NJ |
2633 | |
2634 | The default @code{class-redefinition} method, specialized for classes | |
2635 | with the default metaclass @code{<class>}, has the following internal | |
2636 | protocol. | |
2637 | ||
eb12b401 | 2638 | @code{class-redefinition (@var{old <class>}) (@var{new <class>})} |
a0e07ba4 NJ |
2639 | (method) |
2640 | ||
2641 | @itemize @bullet | |
2642 | @item | |
2643 | @code{remove-class-accessors! @var{old}} (generic) | |
2644 | ||
2645 | @item | |
2646 | @code{update-direct-method! @var{method} @var{old} @var{new}} (generic) | |
2647 | ||
2648 | @item | |
2649 | @code{update-direct-subclass! @var{subclass} @var{old} @var{new}} (generic) | |
2650 | @end itemize | |
2651 | ||
da901526 MD |
2652 | This protocol cleans up things that the definition of the old class |
2653 | once changed and modifies things to work with the new class. | |
2654 | ||
2655 | The default @code{remove-class-accessors!} method removes the | |
2656 | accessor methods of the old class from all classes which they | |
2657 | specialize. | |
2658 | ||
2659 | The default @code{update-direct-method!} method substitutes the new | |
2660 | class for the old in all methods specialized to the old class. | |
2661 | ||
a0e07ba4 | 2662 | The default @code{update-direct-subclass!} method invokes |
da901526 MD |
2663 | @code{class-redefinition} recursively to handle the redefinition of |
2664 | subclasses. | |
a0e07ba4 NJ |
2665 | |
2666 | When a class is redefined, any existing instance of the redefined class | |
2667 | will be modified for the new class definition before the next time that | |
2668 | any of the instance's slot is referenced or set. GOOPS modifies each | |
da901526 | 2669 | instance by calling the generic function @code{change-class}. |
a0e07ba4 NJ |
2670 | |
2671 | The default @code{change-class} method copies slot values from the old | |
ddee39a1 | 2672 | to the modified instance, and initializes new slots, as described in |
a0e07ba4 NJ |
2673 | @ref{Changing the Class of an Instance}. After doing so, it makes a |
2674 | generic function invocation that can be used to customize the instance | |
2675 | update algorithm. | |
2676 | ||
eb12b401 | 2677 | @code{change-class (@var{old-instance <object>}) (@var{new <class>})} (method) |
a0e07ba4 NJ |
2678 | |
2679 | @itemize @bullet | |
2680 | @item | |
2681 | @code{update-instance-for-different-class @var{old-instance} @var{new-instance}} (generic) | |
2682 | ||
2683 | @code{change-class} invokes @code{update-instance-for-different-class} | |
2684 | as the last thing that it does before returning. The applied method can | |
2685 | make any further adjustments to @var{new-instance} that are required to | |
2686 | complete or modify the change of class. The return value from the | |
2687 | applied method is ignored. | |
2688 | ||
2689 | The default @code{update-instance-for-different-class} method does | |
2690 | nothing. | |
2691 | @end itemize | |
2692 | ||
2693 | @node Method Definition | |
eb12b401 | 2694 | @subsection Method Definition |
a0e07ba4 NJ |
2695 | |
2696 | @code{define-method} (syntax) | |
2697 | ||
2698 | @itemize @bullet | |
2699 | @item | |
2700 | @code{add-method! @var{target} @var{method}} (generic) | |
2701 | ||
2702 | @code{define-method} invokes the @code{add-method!} generic function to | |
2703 | handle adding the new method to a variety of possible targets. GOOPS | |
2704 | includes methods to handle @var{target} as | |
2705 | ||
2706 | @itemize @bullet | |
2707 | @item | |
2708 | a generic function (the most common case) | |
2709 | ||
2710 | @item | |
2711 | a procedure | |
2712 | ||
2713 | @item | |
2714 | a primitive generic (@pxref{Extending Guiles Primitives}) | |
2715 | @end itemize | |
2716 | ||
2717 | By defining further methods for @code{add-method!}, you can | |
2718 | theoretically handle adding methods to further types of target. | |
2719 | @end itemize | |
2720 | ||
2721 | @node Generic Function Invocation | |
eb12b401 | 2722 | @subsection Generic Function Invocation |
a0e07ba4 NJ |
2723 | |
2724 | [ *fixme* Description required here. ] | |
2725 | ||
2726 | @code{apply-generic} | |
2727 | ||
2728 | @itemize @bullet | |
2729 | @item | |
2730 | @code{no-method} | |
2731 | ||
2732 | @item | |
2733 | @code{compute-applicable-methods} | |
2734 | ||
2735 | @item | |
2736 | @code{sort-applicable-methods} | |
2737 | ||
2738 | @item | |
2739 | @code{apply-methods} | |
2740 | ||
2741 | @item | |
2742 | @code{no-applicable-method} | |
2743 | @end itemize | |
2744 | ||
2745 | @code{sort-applicable-methods} | |
2746 | ||
2747 | @itemize @bullet | |
2748 | @item | |
2749 | @code{method-more-specific?} | |
2750 | @end itemize | |
2751 | ||
2752 | @code{apply-methods} | |
2753 | ||
2754 | @itemize @bullet | |
2755 | @item | |
2756 | @code{apply-method} | |
2757 | @end itemize | |
2758 | ||
2759 | @code{next-method} | |
2760 | ||
2761 | @itemize @bullet | |
2762 | @item | |
2763 | @code{no-next-method} | |
2764 | @end itemize |