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