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