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