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