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