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