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