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