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