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