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