(Using Guile in Emacs, GDS Introduction):
[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
841initial slot value. @var{init-keyword} is a keyword that can be used to
842pass an initial slot value to @code{make} when creating a new instance.
843
844If more than one of these options is specified for the same slot, the
845order of precedence, highest first is
846
847@itemize @bullet
848@item
849@code{#:init-keyword}, if @var{init-keyword} is present in the options
850passed to @code{make}
851
852@item
853@code{#:init-thunk}, @code{#:init-form} or @code{#:init-value}.
854@end itemize
855
856If the slot definition contains more than one initialization option of
857the same precedence, the later ones are ignored. If a slot is not
858initialized at all, its value is unbound.
859
860In general, slots that are shared between more than one instance are
861only initialized at new instance creation time if the slot value is
862unbound at that time. However, if the new instance creation specifies
863a valid init keyword and value for a shared slot, the slot is
864re-initialized regardless of its previous value.
865
866Note, however, that the power of GOOPS' metaobject protocol means that
867everything written here may be customized or overridden for particular
868classes! The slot initializations described here are performed by the least
869specialized method of the generic function @code{initialize}, whose
870signature is
871
872@example
873(define-method (initialize (object <object>) initargs) ...)
874@end example
875
876The initialization of instances of any given class can be customized by
877defining a @code{initialize} method that is specialized for that class,
878and the author of the specialized method may decide to call
879@code{next-method} - which will result in a call to the next less
880specialized @code{initialize} method - at any point within the
881specialized code, or maybe not at all. In general, therefore, the
882initialization mechanisms described here may be modified or overridden by
883more specialized code, or may not be supported at all for particular
884classes.
885@end deffn
886
887@node Class Definition Internals
888@subsection Class Definition Internals
889
890Implementation notes: @code{define-class} expands to an expression which
891
892@itemize @bullet
893@item
894checks that it is being evaluated only at top level
895
896@item
897defines any accessors that are implied by the @var{slot-definition}s
898
899@item
900uses @code{class} to create the new class (@pxref{Class Definition
901Internals,, class})
902
903@item
904checks for a previous class definition for @var{name} and, if found,
905handles the redefinition by invoking @code{class-redefinition}
906(@pxref{Redefining a Class}).
907@end itemize
908
909@deffn syntax class name (super @dots{}) slot-definition @dots{} . options
910Return a newly created class that inherits from @var{super}s, with
911direct slots defined by @var{slot-definition}s and class options
912@var{options}. For the format of @var{slot-definition}s and
913@var{options}, see @ref{Basic Class Definition,, define-class}.
914@end deffn
915
916Implementation notes: @code{class} expands to an expression which
917
918@itemize @bullet
919@item
920processes the class and slot definition options to check that they are
921well-formed, to convert the @code{#:init-form} option to an
922@code{#:init-thunk} option, to supply a default environment parameter
923(the current top-level environment) and to evaluate all the bits that
924need to be evaluated
925
926@item
927calls @code{make-class} to create the class with the processed and
928evaluated parameters.
929@end itemize
930
931@deffn procedure make-class supers slots . options
932Return a newly created class that inherits from @var{supers}, with
933direct slots defined by @var{slots} and class options @var{options}.
934For the format of @var{slots} and @var{options}, see @ref{Basic Class
935Definition,, define-class}, except note that for @code{make-class},
936@var{slots} and @var{options} are separate list parameters: @var{slots}
937here is a list of slot definitions.
938@end deffn
939
940Implementation notes: @code{make-class}
941
942@itemize @bullet
943@item
944adds @code{<object>} to the @var{supers} list if @var{supers} is empty
945or if none of the classes in @var{supers} have @code{<object>} in their
946class precedence list
947
948@item
949defaults the @code{#:environment}, @code{#:name} and @code{#:metaclass}
950options, if they are not specified by @var{options}, to the current
951top-level environment, the unbound value, and @code{(ensure-metaclass
952@var{supers})} respectively (@pxref{Class Definition Internals,,
953ensure-metaclass})
954
955@item
956checks for duplicate classes in @var{supers} and duplicate slot names in
957@var{slots}, and signals an error if there are any duplicates
958
959@item
960calls @code{make}, passing the metaclass as the first parameter and all
961other parameters as option keywords with values.
962@end itemize
963
964@deffn procedure ensure-metaclass supers env
965Return a metaclass suitable for a class that inherits from the list of
966classes in @var{supers}. The returned metaclass is the union by
967inheritance of the metaclasses of the classes in @var{supers}.
968
969In the simplest case, where all the @var{supers} are straightforward
970classes with metaclass @code{<class>}, the returned metaclass is just
971@code{<class>}.
972
973For a more complex example, suppose that @var{supers} contained one
974class with metaclass @code{<operator-class>} and one with metaclass
975@code{<foreign-object-class>}. Then the returned metaclass would be a
976class that inherits from both @code{<operator-class>} and
977@code{<foreign-object-class>}.
978
979If @var{supers} is the empty list, @code{ensure-metaclass} returns the
980default GOOPS metaclass @code{<class>}.
981
982GOOPS keeps a list of the metaclasses created by
983@code{ensure-metaclass}, so that each required type of metaclass only
984has to be created once.
985
986The @code{env} parameter is ignored.
987@end deffn
988
989@deffn procedure ensure-metaclass-with-supers meta-supers
990@code{ensure-metaclass-with-supers} is an internal procedure used by
991@code{ensure-metaclass} (@pxref{Class Definition Internals,,
992ensure-metaclass}). It returns a metaclass that is the union by
993inheritance of the metaclasses in @var{meta-supers}.
994@end deffn
995
996The internals of @code{make}, which is ultimately used to create the new
997class object, are described in @ref{Customizing Instance Creation},
998which covers the creation and initialization of instances in general.
999
1000@node Customizing Class Definition
1001@subsection Customizing Class Definition
1002
1003During the initialization of a new class, GOOPS calls a number of generic
1004functions with the newly allocated class instance as the first
1005argument. Specifically, GOOPS calls the generic function
1006
1007@itemize @bullet
1008@item
1009(initialize @var{class} @dots{})
1010@end itemize
1011
1012where @var{class} is the newly allocated class instance, and the default
1013@code{initialize} method for arguments of type @code{<class>} calls the
1014generic functions
1015
1016@itemize @bullet
1017@item
1018(compute-cpl @var{class})
1019
1020@item
1021(compute-slots @var{class})
1022
1023@item
1024(compute-get-n-set @var{class} @var{slot-def}), for each of the slot
1025definitions returned by @code{compute-slots}
1026
1027@item
1028(compute-getter-method @var{class} @var{slot-def}), for each of the
1029slot definitions returned by @code{compute-slots} that includes a
1030@code{#:getter} or @code{#:accessor} slot option
1031
1032@item
1033(compute-setter-method @var{class} @var{slot-def}), for each of the
1034slot definitions returned by @code{compute-slots} that includes a
1035@code{#:setter} or @code{#:accessor} slot option.
1036@end itemize
1037
1038If the metaclass of the new class is something more specialized than the
1039default @code{<class>}, then the type of @var{class} in the calls above
1040is more specialized than @code{<class>}, and hence it becomes possible
1041to define generic function methods, specialized for the new class's
1042metaclass, that can modify or override the default behaviour of
1043@code{initialize}, @code{compute-cpl} or @code{compute-get-n-set}.
1044
1045@code{compute-cpl} computes the class precedence list (``CPL'') for the
1046new class (@pxref{Class precedence list}), and returns it as a list of
1047class objects. The CPL is important because it defines a superclass
1048ordering that is used, when a generic function is invoked upon an
1049instance of the class, to decide which of the available generic function
1050methods is the most specific. Hence @code{compute-cpl} could be
1051customized in order to modify the CPL ordering algorithm for all classes
1052with a special metaclass.
1053
1054The default CPL algorithm is encapsulated by the @code{compute-std-cpl}
1055procedure, which is in turn called by the default @code{compute-cpl}
1056method.
1057
1058@deffn procedure compute-std-cpl class
1059Compute and return the class precedence list for @var{class} according
1060to the algorithm described in @ref{Class precedence list}.
1061@end deffn
1062
1063@code{compute-slots} computes and returns a list of all slot definitions
1064for the new class. By default, this list includes the direct slot
1065definitions from the @code{define-class} form, plus the slot definitions
1066that are inherited from the new class's superclasses. The default
1067@code{compute-slots} method uses the CPL computed by @code{compute-cpl}
1068to calculate this union of slot definitions, with the rule that slots
1069inherited from superclasses are shadowed by direct slots with the same
1070name. One possible reason for customizing @code{compute-slots} would be
1071to implement an alternative resolution strategy for slot name conflicts.
1072
1073@code{compute-get-n-set} computes the low-level closures that will be
1074used to get and set the value of a particular slot, and returns them in
1075a list with two elements.
1076
1077The closures returned depend on how storage for that slot is allocated.
1078The standard @code{compute-get-n-set} method, specialized for classes of
1079type @code{<class>}, handles the standard GOOPS values for the
1080@code{#:allocation} slot option (@pxref{Slot Options,, allocation}). By
1081defining a new @code{compute-get-n-set} method for a more specialized
1082metaclass, it is possible to support new types of slot allocation.
1083
1084Suppose you wanted to create a large number of instances of some class
1085with a slot that should be shared between some but not all instances of
1086that class - say every 10 instances should share the same slot storage.
1087The following example shows how to implement and use a new type of slot
1088allocation to do this.
1089
1090@example
1091(define-class <batched-allocation-metaclass> (<class>))
1092
1093(let ((batch-allocation-count 0)
1094 (batch-get-n-set #f))
1095 (define-method (compute-get-n-set (class <batched-allocation-metaclass>) s)
1096 (case (slot-definition-allocation s)
1097 ((#:batched)
1098 ;; If we've already used the same slot storage for 10 instances,
1099 ;; reset variables.
1100 (if (= batch-allocation-count 10)
1101 (begin
1102 (set! batch-allocation-count 0)
1103 (set! batch-get-n-set #f)))
1104 ;; If we don't have a current pair of get and set closures,
1105 ;; create one. make-closure-variable returns a pair of closures
1106 ;; around a single Scheme variable - see goops.scm for details.
1107 (or batch-get-n-set
1108 (set! batch-get-n-set (make-closure-variable)))
1109 ;; Increment the batch allocation count.
1110 (set! batch-allocation-count (+ batch-allocation-count 1))
1111 batch-get-n-set)
1112
1113 ;; Call next-method to handle standard allocation types.
1114 (else (next-method)))))
1115
1116(define-class <class-using-batched-slot> ()
1117 ...
1118 (c #:allocation #:batched)
1119 ...
1120 #:metaclass <batched-allocation-metaclass>)
ddee39a1 1121@end example
a0e07ba4
NJ
1122
1123The usage of @code{compute-getter-method} and @code{compute-setter-method}
1124is described in @ref{MOP Specification}.
1125
1126@code{compute-cpl} and @code{compute-get-n-set} are called by the
1127standard @code{initialize} method for classes whose metaclass is
1128@code{<class>}. But @code{initialize} itself can also be modified, by
1129defining an @code{initialize} method specialized to the new class's
1130metaclass. Such a method could complete override the standard
1131behaviour, by not calling @code{(next-method)} at all, but more
1132typically it would perform additional class initialization steps before
1133and/or after calling @code{(next-method)} for the standard behaviour.
1134
1135@node STKlos Compatibility
1136@subsection STKlos Compatibility
1137
1138If the STKlos compatibility module is loaded, @code{define-class} is
1139overwritten by a STKlos-specific definition; the standard GOOPS
1140definition of @code{define-class} remains available in
1141@code{standard-define-class}.
1142
1143@deffn syntax standard-define-class name (super @dots{}) slot-definition @dots{} . options
1144@code{standard-define-class} is equivalent to the standard GOOPS
1145@code{define-class}.
1146@end deffn
1147
1148@node Creating Instances
1149@section Creating Instances
1150
1151@menu
1152* Basic Instance Creation::
1153* Customizing Instance Creation::
1154@end menu
1155
1156@node Basic Instance Creation
1157@subsection Basic Instance Creation
1158
1159To create a new instance of any GOOPS class, use the generic function
1160@code{make} or @code{make-instance}, passing the required class and any
1161appropriate instance initialization arguments as keyword and value
1162pairs. Note that @code{make} and @code{make-instances} are aliases for
1163each other - their behaviour is identical.
1164
1165@deffn generic make
1166@deffnx method make (class <class>) . initargs
1167Create and return a new instance of class @var{class}, initialized using
1168@var{initargs}.
1169
1170In theory, @var{initargs} can have any structure that is understood by
1171whatever methods get applied when the @code{initialize} generic function
1172is applied to the newly allocated instance.
1173
1174In practice, specialized @code{initialize} methods would normally call
1175@code{(next-method)}, and so eventually the standard GOOPS
1176@code{initialize} methods are applied. These methods expect
1177@var{initargs} to be a list with an even number of elements, where
1178even-numbered elements (counting from zero) are keywords and
1179odd-numbered elements are the corresponding values.
1180
1181GOOPS processes initialization argument keywords automatically for slots
1182whose definition includes the @code{#:init-keyword} option (@pxref{Slot
1183Options,, init-keyword}). Other keyword value pairs can only be
1184processed by an @code{initialize} method that is specialized for the new
1185instance's class. Any unprocessed keyword value pairs are ignored.
1186@end deffn
1187
1188@deffn generic make-instance
1189@deffnx method make-instance (class <class>) . initargs
1190@code{make-instance} is an alias for @code{make}.
1191@end deffn
1192
1193@node Customizing Instance Creation
1194@subsection Customizing Instance Creation
1195
1196@code{make} itself is a generic function. Hence the @code{make}
1197invocation itself can be customized in the case where the new instance's
1198metaclass is more specialized than the default @code{<class>}, by
1199defining a @code{make} method that is specialized to that metaclass.
1200
1201Normally, however, the method for classes with metaclass @code{<class>}
1202will be applied. This method calls two generic functions:
1203
1204@itemize @bullet
1205@item
1206(allocate-instance @var{class} . @var{initargs})
1207
1208@item
1209(initialize @var{instance} . @var{initargs})
1210@end itemize
1211
1212@code{allocate-instance} allocates storage for and returns the new
1213instance, uninitialized. You might customize @code{allocate-instance},
1214for example, if you wanted to provide a GOOPS wrapper around some other
1215object programming system.
1216
1217To do this, you would create a specialized metaclass, which would act as
1218the metaclass for all classes and instances from the other system. Then
1219define an @code{allocate-instance} method, specialized to that
1220metaclass, which calls a Guile primitive C function, which in turn
1221allocates the new instance using the interface of the other object
1222system.
1223
1224In this case, for a complete system, you would also need to customize a
1225number of other generic functions like @code{make} and
1226@code{initialize}, so that GOOPS knows how to make classes from the
1227other system, access instance slots, and so on.
1228
1229@code{initialize} initializes the instance that is returned by
1230@code{allocate-instance}. The standard GOOPS methods perform
1231initializations appropriate to the instance class.
1232
1233@itemize @bullet
1234@item
1235At the least specialized level, the method for instances of type
1236@code{<object>} performs internal GOOPS instance initialization, and
1237initializes the instance's slots according to the slot definitions and
1238any slot initialization keywords that appear in @var{initargs}.
1239
1240@item
1241The method for instances of type @code{<class>} calls
1242@code{(next-method)}, then performs the class initializations described
1243in @ref{Customizing Class Definition}.
1244
1245@item
1246and so on for generic functions, method, operator classes @dots{}
1247@end itemize
1248
1249Similarly, you can customize the initialization of instances of any
1250application-defined class by defining an @code{initialize} method
1251specialized to that class.
1252
1253Imagine a class whose instances' slots need to be initialized at
1254instance creation time by querying a database. Although it might be
1255possible to achieve this a combination of @code{#:init-thunk} keywords
1256and closures in the slot definitions, it is neater to write an
1257@code{initialize} method for the class that queries the database once
1258and initializes all the dependent slot values according to the results.
1259
1260@node Accessing Slots
1261@section Accessing Slots
1262
1263The definition of a slot contains at the very least a slot name, and may
1264also contain various slot options, including getter, setter and/or
1265accessor functions for the slot.
1266
1267It is always possible to access slots by name, using the various
1268``slot-ref'' and ``slot-set!'' procedures described in the following
1269subsections. For example,
1270
1271@example
1272(define-class <my-class> () ;; Define a class with slots
1273 (count #:init-value 0) ;; named "count" and "cache".
1274 (cache #:init-value '())
1275 @dots{})
1276
1277(define inst (make <my-class>)) ;; Make an instance of this class.
1278
1279(slot-set! inst 'count 5) ;; Set the value of the "count"
1280 ;; slot to 5.
1281
1282(slot-set! inst 'cache ;; Modify the value of the
1283 (cons (cons "^it" "It") ;; "cache" slot.
1284 (slot-ref inst 'cache)))
1285@end example
1286
1287If a slot definition includes a getter, setter or accessor function,
1288these can be used instead of @code{slot-ref} and @code{slot-set!} to
1289access the slot.
1290
1291@example
1292(define-class <adv-class> () ;; Define a new class whose slots
1293 (count #:setter set-count) ;; use a getter, a setter and
1294 (cache #:accessor cache) ;; an accessor.
1295 (csize #:getter cache-size)
1296 @dots{})
1297
1298(define inst (make <adv-class>)) ;; Make an instance of this class.
1299
1300(set-count inst 5) ;; Set the value of the "count"
1301 ;; slot to 5.
1302
1303(set! (cache inst) ;; Modify the value of the
1304 (cons (cons "^it" "It") ;; "cache" slot.
1305 (cache inst)))
1306
1307(let ((size (cache-size inst))) ;; Get the value of the "csize"
1308 @dots{}) ;; slot.
1309@end example
1310
1311Whichever of these methods is used to access slots, GOOPS always calls
1312the low-level @dfn{getter} and @dfn{setter} closures for the slot to get
1313and set its value. These closures make sure that the slot behaves
1314according to the @code{#:allocation} type that was specified in the slot
1315definition (@pxref{Slot Options,, allocation}). (For more about these
1316closures, see @ref{Customizing Class Definition,, compute-get-n-set}.)
1317
1318@menu
1319* Instance Slots::
1320* Class Slots::
1321* Handling Slot Access Errors::
1322@end menu
1323
1324@node Instance Slots
1325@subsection Instance Slots
1326
1327Any slot, regardless of its allocation, can be queried, referenced and
1328set using the following four primitive procedures.
1329
1330@deffn {primitive procedure} slot-exists? obj slot-name
1331Return @code{#t} if @var{obj} has a slot with name @var{slot-name},
1332otherwise @code{#f}.
1333@end deffn
1334
1335@deffn {primitive procedure} slot-bound? obj slot-name
1336Return @code{#t} if the slot named @var{slot-name} in @var{obj} has a
1337value, otherwise @code{#f}.
1338
1339@code{slot-bound?} calls the generic function @code{slot-missing} if
1340@var{obj} does not have a slot called @var{slot-name} (@pxref{Handling
1341Slot Access Errors, slot-missing}).
1342@end deffn
1343
1344@deffn {primitive procedure} slot-ref obj slot-name
1345Return the value of the slot named @var{slot-name} in @var{obj}.
1346
1347@code{slot-ref} calls the generic function @code{slot-missing} if
1348@var{obj} does not have a slot called @var{slot-name} (@pxref{Handling
1349Slot Access Errors, slot-missing}).
1350
1351@code{slot-ref} calls the generic function @code{slot-unbound} if the
1352named slot in @var{obj} does not have a value (@pxref{Handling Slot
1353Access Errors, slot-unbound}).
1354@end deffn
1355
1356@deffn {primitive procedure} slot-set! obj slot-name value
1357Set the value of the slot named @var{slot-name} in @var{obj} to @var{value}.
1358
1359@code{slot-set!} calls the generic function @code{slot-missing} if
1360@var{obj} does not have a slot called @var{slot-name} (@pxref{Handling
1361Slot Access Errors, slot-missing}).
1362@end deffn
1363
1364GOOPS stores information about slots in class metaobjects. Internally,
1365all of these procedures work by looking up the slot definition for the
1366slot named @var{slot-name} in the class metaobject for @code{(class-of
1367@var{obj})}, and then using the slot definition's ``getter'' and
1368``setter'' closures to get and set the slot value.
1369
1370The next four procedures differ from the previous ones in that they take
1371the class metaobject as an explicit argument, rather than assuming
1372@code{(class-of @var{obj})}. Therefore they allow you to apply the
1373``getter'' and ``setter'' closures of a slot definition in one class to
1374an instance of a different class.
1375
1376[ *fixme* I have no idea why this is useful! Perhaps when a slot in
1377@code{(class-of @var{obj})} shadows a slot with the same name in one of
1378its superclasses? There should be an enlightening example here. ]
1379
1380@deffn {primitive procedure} slot-exists-using-class? class obj slot-name
1381Return @code{#t} if the class metaobject @var{class} has a slot
1382definition for a slot with name @var{slot-name}, otherwise @code{#f}.
1383@end deffn
1384
1385@deffn {primitive procedure} slot-bound-using-class? class obj slot-name
1386Return @code{#t} if applying @code{slot-ref-using-class} to the same
1387arguments would call the generic function @code{slot-unbound}, otherwise
1388@code{#f}.
1389
1390@code{slot-bound-using-class?} calls the generic function
1391@code{slot-missing} if @var{class} does not have a slot definition for a
1392slot called @var{slot-name} (@pxref{Handling Slot Access Errors,
1393slot-missing}).
1394@end deffn
1395
1396@deffn {primitive procedure} slot-ref-using-class class obj slot-name
1397Apply the ``getter'' closure for the slot named @var{slot-name} in
1398@var{class} to @var{obj}, and return its result.
1399
1400@code{slot-ref-using-class} calls the generic function
1401@code{slot-missing} if @var{class} does not have a slot definition for a
1402slot called @var{slot-name} (@pxref{Handling Slot Access Errors,
1403slot-missing}).
1404
1405@code{slot-ref-using-class} calls the generic function
1406@code{slot-unbound} if the application of the ``getter'' closure to
1407@var{obj} returns an unbound value (@pxref{Handling Slot Access Errors,
1408slot-unbound}).
1409@end deffn
1410
1411@deffn {primitive procedure} slot-set-using-class! class obj slot-name value
1412Apply the ``setter'' closure for the slot named @var{slot-name} in
1413@var{class} to @var{obj} and @var{value}.
1414
1415@code{slot-set-using-class!} calls the generic function
1416@code{slot-missing} if @var{class} does not have a slot definition for a
1417slot called @var{slot-name} (@pxref{Handling Slot Access Errors,
1418slot-missing}).
1419@end deffn
1420
1421@node Class Slots
1422@subsection Class Slots
1423
1424Slots whose allocation is per-class rather than per-instance can be
1425referenced and set without needing to specify any particular instance.
1426
1427@deffn procedure class-slot-ref class slot-name
1428Return the value of the slot named @var{slot-name} in class @var{class}.
1429The named slot must have @code{#:class} or @code{#:each-subclass}
1430allocation (@pxref{Slot Options,, allocation}).
1431
1432If there is no such slot with @code{#:class} or @code{#:each-subclass}
1433allocation, @code{class-slot-ref} calls the @code{slot-missing} generic
1434function with arguments @var{class} and @var{slot-name}. Otherwise, if
1435the slot value is unbound, @code{class-slot-ref} calls the
1436@code{slot-missing} generic function, with the same arguments.
1437@end deffn
1438
1439@deffn procedure class-slot-set! class slot-name value
1440Set the value of the slot named @var{slot-name} in class @var{class} to
1441@var{value}. The named slot must have @code{#:class} or
1442@code{#:each-subclass} allocation (@pxref{Slot Options,, allocation}).
1443
1444If there is no such slot with @code{#:class} or @code{#:each-subclass}
1445allocation, @code{class-slot-ref} calls the @code{slot-missing} generic
1446function with arguments @var{class} and @var{slot-name}.
1447@end deffn
1448
1449@node Handling Slot Access Errors
1450@subsection Handling Slot Access Errors
1451
1452GOOPS calls one of the following generic functions when a ``slot-ref''
1453or ``slot-set!'' call specifies a non-existent slot name, or tries to
1454reference a slot whose value is unbound.
1455
1456@deffn generic slot-missing
1457@deffnx method slot-missing (class <class>) slot-name
1458@deffnx method slot-missing (class <class>) (object <object>) slot-name
1459@deffnx method slot-missing (class <class>) (object <object>) slot-name value
1460When an application attempts to reference or set a class or instance
1461slot by name, and the slot name is invalid for the specified @var{class}
1462or @var{object}, GOOPS calls the @code{slot-missing} generic function.
1463
1464The default methods all call @code{goops-error} with an appropriate
1465message.
1466@end deffn
1467
1468@deffn generic slot-unbound
1469@deffnx method slot-unbound (object <object>)
1470@deffnx method slot-unbound (class <class>) slot-name
1471@deffnx method slot-unbound (class <class>) (object <object>) slot-name
1472When an application attempts to reference a class or instance slot, and
1473the slot's value is unbound, GOOPS calls the @code{slot-unbound} generic
1474function.
1475
1476The default methods all call @code{goops-error} with an appropriate
1477message.
1478@end deffn
1479
1480@node Creating Generic Functions
1481@section Creating Generic Functions
1482
1483A generic function is a collection of methods, with rules for
1484determining which of the methods should be applied for any given
1485invocation of the generic function.
1486
1487GOOPS represents generic functions as metaobjects of the class
1488@code{<generic>} (or one of its subclasses).
1489
1490@menu
1491* Basic Generic Function Creation::
1492* Generic Function Internals::
1493* Extending Guiles Primitives::
1494@end menu
1495
1496@node Basic Generic Function Creation
1497@subsection Basic Generic Function Creation
1498
1499The following forms may be used to bind a variable to a generic
1500function. Depending on that variable's pre-existing value, the generic
1501function may be created empty - with no methods - or it may contain
1502methods that are inferred from the pre-existing value.
1503
1504It is not, in general, necessary to use @code{define-generic} or
1505@code{define-accessor} before defining methods for the generic function
1506using @code{define-method}, since @code{define-method} will
1507automatically interpolate a @code{define-generic} call, or upgrade an
1508existing generic to an accessor, if that is implied by the
1509@code{define-method} call. Note in particular that,
1510if the specified variable already has a @emph{generic function} value,
1511@code{define-generic} and @code{define-accessor} will @emph{discard} it!
1512Obviously it is application-dependent whether this is desirable or not.
1513
1514If, for example, you wanted to extend @code{+} for a class representing
1515a new numerical type, you probably want to inherit any existing methods
1516for @code{+} and so should not use @code{define-generic}. If, on the
1517other hand, you do not want to risk inheriting methods whose behaviour
1518might surprise you, you can use @code{define-generic} or
1519@code{define-accessor} to wipe the slate clean.
1520
1521@deffn syntax define-generic symbol
1522Create a generic function with name @var{symbol} and bind it to the
1523variable @var{symbol}.
1524
1525If the variable @var{symbol} was previously bound to a Scheme procedure
1526(or procedure-with-setter), the old procedure (and setter) is
1527incorporated into the new generic function as its default procedure (and
1528setter). Any other previous value that was bound to @var{symbol},
1529including an existing generic function, is overwritten by the new
1530generic function.
1531@end deffn
1532
1533@deffn syntax define-accessor symbol
1534Create an accessor with name @var{symbol} and bind it to the variable
1535@var{symbol}.
1536
1537If the variable @var{symbol} was previously bound to a Scheme procedure
1538(or procedure-with-setter), the old procedure (and setter) is
1539incorporated into the new accessor as its default procedure (and
1540setter). Any other previous value that was bound to @var{symbol},
1541including an existing generic function or accessor, is overwritten by
1542the new definition.
1543@end deffn
1544
31a4ff3e
MV
1545It is sometimes tempting to use GOOPS accessors with short names. For
1546example, it is tempting to use the name @code{x} for the x-coordinate
1547in vector packages.
1548
1549Assume that we work with a graphical package which needs to use two
1550independent vector packages for 2D and 3D vectors respectively. If
1551both packages export @code{x} we will encounter a name collision.
1552
1553This can be resolved automagically with the duplicates handler
1554@code{merge-generics} which gives the module system license to merge
1555all generic functions sharing a common name:
1556
1557@smalllisp
1558(define-module (math 2D-vectors)
1559 :use-module (oop goops)
1560 :export (x y ...))
1561
1562(define-module (math 3D-vectors)
1563 :use-module (oop goops)
1564 :export (x y z ...))
1565
1566(define-module (my-module)
1567 :use-module (math 2D-vectors)
1568 :use-module (math 3D-vectors)
1569 :duplicates merge-generics)
1570@end smalllisp
1571
1572The generic function @code{x} in @code{(my-module)} will now share
1573methods with @code{x} in both imported modules.
1574
1575There will, in fact, now be three distinct generic functions named
1576@code{x}: @code{x} in @code{(2D-vectors)}, @code{x} in
1577@code{(3D-vectors)}, and @code{x} in @code{(my-module)}. The last
1578function will be an @code{<extended-generic>}, extending the previous
1579two functions.
1580
1581Let's call the imported generic functions the "ancestor functions".
1582The generic function @code{x} in @code{(my-module)} is, in turn, a
1583"descendant function" of the imported functions, extending its
1584ancestors.
1585
1586For any generic function G, the applicable methods are selected from
1587the union of the methods of the descendant functions, the methods of G
1588itself and the methods of the ancestor functions.
1589
1590This, ancestor functions share methods with their descendants and vice
1591versa. This implies that @code{x} in @code{(math 2D-vectors)} will
1592share the methods of @code{x} in @code{(my-module)} and vice versa,
1593while @code{x} in @code{(math 2D-vectors)} doesn't share the methods
1594of @code{x} in @code{(math 3D-vectors)}, thus preserving modularity.
1595
1596Sharing is dynamic, so that adding new methods to a descendant implies
1597adding it to the ancestor.
1598
1599If duplicates checking is desired in the above example, the following
1600form of the @code{:duplicates} option can be used instead:
1601
1602@smalllisp
1603 :duplicates (merge-generics check)
1604@end smalllisp
1605
a0e07ba4
NJ
1606@node Generic Function Internals
1607@subsection Generic Function Internals
1608
1609@code{define-generic} calls @code{ensure-generic} to upgrade a
1610pre-existing procedure value, or @code{make} with metaclass
1611@code{<generic>} to create a new generic function.
1612
1613@code{define-accessor} calls @code{ensure-accessor} to upgrade a
1614pre-existing procedure value, or @code{make-accessor} to create a new
1615accessor.
1616
1617@deffn procedure ensure-generic old-definition [name]
1618Return a generic function with name @var{name}, if possible by using or
1619upgrading @var{old-definition}. If unspecified, @var{name} defaults to
1620@code{#f}.
1621
1622If @var{old-definition} is already a generic function, it is returned
1623unchanged.
1624
1625If @var{old-definition} is a Scheme procedure or procedure-with-setter,
1626@code{ensure-generic} returns a new generic function that uses
1627@var{old-definition} for its default procedure and setter.
1628
1629Otherwise @code{ensure-generic} returns a new generic function with no
1630defaults and no methods.
1631@end deffn
1632
1633@deffn procedure make-generic [name]
1634Return a new generic function with name @code{(car @var{name})}. If
1635unspecified, @var{name} defaults to @code{#f}.
1636@end deffn
1637
1638@code{ensure-generic} calls @code{make} with metaclasses
1639@code{<generic>} and @code{<generic-with-setter>}, depending on the
1640previous value of the variable that it is trying to upgrade.
1641
1642@code{make-generic} is a simple wrapper for @code{make} with metaclass
1643@code{<generic>}.
1644
1645@deffn procedure ensure-accessor proc [name]
1646Return an accessor with name @var{name}, if possible by using or
1647upgrading @var{proc}. If unspecified, @var{name} defaults to @code{#f}.
1648
1649If @var{proc} is already an accessor, it is returned unchanged.
1650
1651If @var{proc} is a Scheme procedure, procedure-with-setter or generic
1652function, @code{ensure-accessor} returns an accessor that reuses the
1653reusable elements of @var{proc}.
1654
1655Otherwise @code{ensure-accessor} returns a new accessor with no defaults
1656and no methods.
1657@end deffn
1658
1659@deffn procedure make-accessor [name]
1660Return a new accessor with name @code{(car @var{name})}. If
1661unspecified, @var{name} defaults to @code{#f}.
1662@end deffn
1663
1664@code{ensure-accessor} calls @code{make} with
1665metaclass @code{<generic-with-setter>}, as well as calls to
1666@code{ensure-generic}, @code{make-accessor} and (tail recursively)
1667@code{ensure-accessor}.
1668
1669@code{make-accessor} calls @code{make} twice, first
1670with metaclass @code{<generic>} to create a generic function for the
1671setter, then with metaclass @code{<generic-with-setter>} to create the
1672accessor, passing the setter generic function as the value of the
1673@code{#:setter} keyword.
1674
1675@node Extending Guiles Primitives
1676@subsection Extending Guile's Primitives
1677
1678When GOOPS is loaded, many of Guile's primitive procedures can be
1679extended by giving them a generic function definition that operates
1680in conjunction with their normal C-coded implementation. For
1681primitives that are extended in this way, the result from the user-
1682or application-level point of view is that the extended primitive
1683behaves exactly like a generic function with the C-coded implementation
1684as its default method.
1685
1686The @code{generic-capability?} predicate should be used to determine
1687whether a particular primitive is extensible in this way.
1688
1689@deffn {primitive procedure} generic-capability? primitive
1690Return @code{#t} if @var{primitive} can be extended by giving it a
1691generic function definition, otherwise @code{#f}.
1692@end deffn
1693
1694Even when a primitive procedure is extensible like this, its generic
1695function definition is not created until it is needed by a call to
1696@code{define-method}, or until the application explicitly requests it
1697by calling @code{enable-primitive-generic!}.
1698
1699@deffn {primitive procedure} enable-primitive-generic! primitive
1700Force the creation of a generic function definition for
1701@var{primitive}.
1702@end deffn
1703
1704Once the generic function definition for a primitive has been created,
1705it can be retrieved using @code{primitive-generic-generic}.
1706
1707@deffn {primitive procedure} primitive-generic-generic primitive
1708Return the generic function definition of @var{primitive}.
1709
1710@code{primitive-generic-generic} raises an error if @var{primitive}
1711is not a primitive with generic capability, or if its generic capability
1712has not yet been enabled, whether implicitly (by @code{define-method})
1713or explicitly (by @code{enable-primitive-generic!}).
1714@end deffn
1715
1716Note that the distinction between, on the one hand, primitives with
1717additional generic function definitions and, on the other hand, generic
1718functions with a default method, may disappear when GOOPS is fully
1719integrated into the core of Guile. Consequently, the
1720procedures described in this section may disappear as well.
1721
1722@node Adding Methods to Generic Functions
1723@section Adding Methods to Generic Functions
1724
1725@menu
1726* Basic Method Definition::
1727* Method Definition Internals::
1728@end menu
1729
1730@node Basic Method Definition
1731@subsection Basic Method Definition
1732
1733To add a method to a generic function, use the @code{define-method} form.
1734
1735@deffn syntax define-method (generic parameter @dots{}) . body
1736Define a method for the generic function or accessor @var{generic} with
1737parameters @var{parameter}s and body @var{body}.
1738
1739@var{generic} is a generic function. If @var{generic} is a variable
1740which is not yet bound to a generic function object, the expansion of
1741@code{define-method} will include a call to @code{define-generic}. If
1742@var{generic} is @code{(setter @var{generic-with-setter})}, where
1743@var{generic-with-setter} is a variable which is not yet bound to a
1744generic-with-setter object, the expansion will include a call to
1745@code{define-accessor}.
1746
1747Each @var{parameter} must be either a symbol or a two-element list
1748@code{(@var{symbol} @var{class})}. The symbols refer to variables in
1749the @var{body} that will be bound to the parameters supplied by the
1750caller when calling this method. The @var{class}es, if present,
1751specify the possible combinations of parameters to which this method
1752can be applied.
1753
1754@var{body} is the body of the method definition.
1755@end deffn
1756
1757@code{define-method} expressions look a little like normal Scheme
1758procedure definitions of the form
1759
1760@example
1761(define (name formals @dots{}) . body)
1762@end example
1763
1764The most important difference is that each formal parameter, apart from the
1765possible ``rest'' argument, can be qualified by a class name:
1766@code{@var{formal}} becomes @code{(@var{formal} @var{class})}. The
1767meaning of this qualification is that the method being defined
1768will only be applicable in a particular generic function invocation if
1769the corresponding argument is an instance of @code{@var{class}} (or one of
1770its subclasses). If more than one of the formal parameters is qualified
1771in this way, then the method will only be applicable if each of the
1772corresponding arguments is an instance of its respective qualifying class.
1773
1774Note that unqualified formal parameters act as though they are qualified
1775by the class @code{<top>}, which GOOPS uses to mean the superclass of
1776all valid Scheme types, including both primitive types and GOOPS classes.
1777
1778For example, if a generic function method is defined with
1779@var{parameter}s @code{((s1 <square>) (n <number>))}, that method is
1780only applicable to invocations of its generic function that have two
1781parameters where the first parameter is an instance of the
1782@code{<square>} class and the second parameter is a number.
1783
1784If a generic function is invoked with a combination of parameters for which
1785there is no applicable method, GOOPS raises an error. For more about
1786invocation error handling, and generic function invocation in general,
1787see @ref{Invoking Generic Functions}.
1788
1789@node Method Definition Internals
1790@subsection Method Definition Internals
1791
1792@code{define-method}
1793
1794@itemize @bullet
1795@item
1796checks the form of the first parameter, and applies the following steps
1797to the accessor's setter if it has the @code{(setter @dots{})} form
1798
1799@item
1800interpolates a call to @code{define-generic} or @code{define-accessor}
1801if a generic function is not already defined with the supplied name
1802
1803@item
1804calls @code{method} with the @var{parameter}s and @var{body}, to make a
1805new method instance
1806
1807@item
1808calls @code{add-method!} to add this method to the relevant generic
1809function.
1810@end itemize
1811
1812@deffn syntax method (parameter @dots{}) . body
1813Make a method whose specializers are defined by the classes in
1814@var{parameter}s and whose procedure definition is constructed from the
1815@var{parameter} symbols and @var{body} forms.
1816
1817The @var{parameter} and @var{body} parameters should be as for
1818@code{define-method} (@pxref{Basic Method Definition,, define-method}).
1819@end deffn
1820
1821@code{method}
1822
1823@itemize @bullet
1824@item
1825extracts formals and specializing classes from the @var{parameter}s,
1826defaulting the class for unspecialized parameters to @code{<top>}
1827
1828@item
1829creates a closure using the formals and the @var{body} forms
1830
1831@item
1832calls @code{make} with metaclass @code{<method>} and the specializers
1833and closure using the @code{#:specializers} and @code{#:procedure}
1834keywords.
1835@end itemize
1836
1837@deffn procedure make-method specializers procedure
1838Make a method using @var{specializers} and @var{procedure}.
1839
1840@var{specializers} should be a list of classes that specifies the
1841parameter combinations to which this method will be applicable.
1842
1843@var{procedure} should be the closure that will applied to the generic
1844function parameters when this method is invoked.
1845@end deffn
1846
1847@code{make-method} is a simple wrapper around @code{make} with metaclass
1848@code{<method>}.
1849
1850@deffn generic add-method! target method
1851Generic function for adding method @var{method} to @var{target}.
1852@end deffn
1853
1854@deffn method add-method! (generic <generic>) (method <method>)
1855Add method @var{method} to the generic function @var{generic}.
1856@end deffn
1857
1858@deffn method add-method! (proc <procedure>) (method <method>)
1859If @var{proc} is a procedure with generic capability (@pxref{Extending
1860Guiles Primitives,, generic-capability?}), upgrade it to a
1861primitive generic and add @var{method} to its generic function
1862definition.
1863@end deffn
1864
1865@deffn method add-method! (pg <primitive-generic>) (method <method>)
1866Add method @var{method} to the generic function definition of @var{pg}.
1867
1868Implementation: @code{(add-method! (primitive-generic-generic pg) method)}.
1869@end deffn
1870
1871@deffn method add-method! (whatever <top>) (method <method>)
1872Raise an error indicating that @var{whatever} is not a valid generic
1873function.
1874@end deffn
1875
1876@node Invoking Generic Functions
1877@section Invoking Generic Functions
1878
1879When a variable with a generic function definition appears as the first
1880element of a list that is being evaluated, the Guile evaluator tries
1881to apply the generic function to the arguments obtained by evaluating
1882the remaining elements of the list. [ *fixme* How do I put this in a
1883more Schemely and less Lispy way? ]
1884
1885Usually a generic function contains several method definitions, with
1886varying degrees of formal parameter specialization (@pxref{Basic
1887Method Definition,, define-method}). So it is necessary to sort these
1888methods by specificity with respect to the supplied arguments, and then
1889apply the most specific method definition. Less specific methods
1890may be applied subsequently if a method that is being applied calls
1891@code{next-method}.
1892
1893@menu
1894* Determining Which Methods to Apply::
1895* Handling Invocation Errors::
1896@end menu
1897
1898@node Determining Which Methods to Apply
1899@subsection Determining Which Methods to Apply
1900
1901[ *fixme* Sorry - this is the area of GOOPS that I understand least of
1902all, so I'm afraid I have to pass on this section. Would some other
1903kind person consider filling it in? ]
1904
1905@deffn generic apply-generic
1906@deffnx method apply-generic (gf <generic>) args
1907@end deffn
1908
1909@deffn generic compute-applicable-methods
1910@deffnx method compute-applicable-methods (gf <generic>) args
1911@end deffn
1912
1913@deffn generic sort-applicable-methods
1914@deffnx method sort-applicable-methods (gf <generic>) methods args
1915@end deffn
1916
1917@deffn generic method-more-specific?
1918@deffnx method method-more-specific? (m1 <method>) (m2 <method>) args
1919@end deffn
1920
1921@deffn generic apply-method
1922@deffnx method apply-method (gf <generic>) methods build-next args
1923@end deffn
1924
1925@deffn generic apply-methods
1926@deffnx method apply-methods (gf <generic>) (l <list>) args
1927@end deffn
1928
1929@node Handling Invocation Errors
1930@subsection Handling Invocation Errors
1931
1932@deffn generic no-method
1933@deffnx method no-method (gf <generic>) args
1934When an application invokes a generic function, and no methods at all
1935have been defined for that generic function, GOOPS calls the
1936@code{no-method} generic function. The default method calls
1937@code{goops-error} with an appropriate message.
1938@end deffn
1939
1940@deffn generic no-applicable-method
1941@deffnx method no-applicable-method (gf <generic>) args
1942When an application applies a generic function to a set of arguments,
1943and no methods have been defined for those argument types, GOOPS calls
1944the @code{no-applicable-method} generic function. The default method
1945calls @code{goops-error} with an appropriate message.
1946@end deffn
1947
1948@deffn generic no-next-method
1949@deffnx method no-next-method (gf <generic>) args
1950When a generic function method calls @code{(next-method)} to invoke the
1951next less specialized method for that generic function, and no less
1952specialized methods have been defined for the current generic function
1953arguments, GOOPS calls the @code{no-next-method} generic function. The
1954default method calls @code{goops-error} with an appropriate message.
1955@end deffn
1956
1957@node Redefining a Class
1958@section Redefining a Class
1959
1960Suppose that a class @code{<my-class>} is defined using @code{define-class}
1961(@pxref{Basic Class Definition,, define-class}), with slots that have
1962accessor functions, and that an application has created several instances
1963of @code{<my-class>} using @code{make} (@pxref{Basic Instance Creation,,
1964make}). What then happens if @code{<my-class>} is redefined by calling
1965@code{define-class} again?
1966
1967@menu
1968* Default Class Redefinition Behaviour::
1969* Customizing Class Redefinition::
1970@end menu
1971
1972@node Default Class Redefinition Behaviour
1973@subsection Default Class Redefinition Behaviour
1974
1975GOOPS' default answer to this question is as follows.
1976
1977@itemize @bullet
1978@item
1979All existing direct instances of @code{<my-class>} are converted to be
1980instances of the new class. This is achieved by preserving the values
1981of slots that exist in both the old and new definitions, and initializing the
1982values of new slots in the usual way (@pxref{Basic Instance Creation,,
1983make}).
1984
1985@item
1986All existing subclasses of @code{<my-class>} are redefined, as though
1987the @code{define-class} expressions that defined them were re-evaluated
1988following the redefinition of @code{<my-class>}, and the class
1989redefinition process described here is applied recursively to the
1990redefined subclasses.
1991
1992@item
1993Once all of its instances and subclasses have been updated, the class
1994metaobject previously bound to the variable @code{<my-class>} is no
1995longer needed and so can be allowed to be garbage collected.
1996@end itemize
1997
1998To keep things tidy, GOOPS also needs to do a little housekeeping on
1999methods that are associated with the redefined class.
2000
2001@itemize @bullet
2002@item
2003Slot accessor methods for slots in the old definition should be removed
2004from their generic functions. They will be replaced by accessor methods
2005for the slots of the new class definition.
2006
2007@item
2008Any generic function method that uses the old @code{<my-class>} metaobject
2009as one of its formal parameter specializers must be updated to refer to
2010the new @code{<my-class>} metaobject. (Whenever a new generic function
2011method is defined, @code{define-method} adds the method to a list stored
2012in the class metaobject for each class used as a formal parameter
2013specializer, so it is easy to identify all the methods that must be
2014updated when a class is redefined.)
2015@end itemize
2016
2017If this class redefinition strategy strikes you as rather counter-intuitive,
2018bear in mind that it is derived from similar behaviour in other object
2019systems such as CLOS, and that experience in those systems has shown it to be
2020very useful in practice.
2021
2022Also bear in mind that, like most of GOOPS' default behaviour, it can
2023be customized@dots{}
2024
2025@node Customizing Class Redefinition
2026@subsection Customizing Class Redefinition
2027
2028When @code{define-class} notices that a class is being redefined,
2029it constructs the new class metaobject as usual, and then invokes the
2030@code{class-redefinition} generic function with the old and new classes
2031as arguments. Therefore, if the old or new classes have metaclasses
2032other than the default @code{<class>}, class redefinition behaviour can
2033be customized by defining a @code{class-redefinition} method that is
2034specialized for the relevant metaclasses.
2035
2036@deffn generic class-redefinition
2037Handle the class redefinition from @var{old-class} to @var{new-class},
2038and return the new class metaobject that should be bound to the
2039variable specified by @code{define-class}'s first argument.
2040@end deffn
2041
2042@deffn method class-redefinition (old-class <class>) (new-class <class>)
2043Implements GOOPS' default class redefinition behaviour, as described in
2044@ref{Default Class Redefinition Behaviour}. Returns the metaobject
2045for the new class definition.
2046@end deffn
2047
2048An alternative class redefinition strategy could be to leave all
2049existing instances as instances of the old class, but accepting that the
2050old class is now ``nameless'', since its name has been taken over by the
2051new definition. In this strategy, any existing subclasses could also
2052be left as they are, on the understanding that they inherit from a nameless
2053superclass.
2054
2055This strategy is easily implemented in GOOPS, by defining a new metaclass,
2056that will be used as the metaclass for all classes to which the strategy
2057should apply, and then defining a @code{class-redefinition} method that
2058is specialized for this metaclass:
2059
2060@example
2061(define-class <can-be-nameless> (<class>))
2062
2063(define-method (class-redefinition (old <can-be-nameless>) (new <class>))
2064 new)
2065@end example
2066
2067When customization can be as easy as this, aren't you glad that GOOPS
2068implements the far more difficult strategy as its default!
2069
2070Finally, note that, if @code{class-redefinition} itself is not customized,
2071the default @code{class-redefinition} method invokes three further
2072generic functions that could be individually customized:
2073
2074@itemize @bullet
2075@item
2076(remove-class-accessors! @var{old-class})
2077
2078@item
2079(update-direct-method! @var{method} @var{old-class} @var{new-class})
2080
2081@item
2082(update-direct-subclass! @var{subclass} @var{old-class} @var{new-class})
2083@end itemize
2084
2085and the default methods for these generic functions invoke further
2086generic functions, and so on@dots{} The detailed protocol for all of these
2087is described in @ref{MOP Specification}.
2088
2089@node Changing the Class of an Instance
2090@section Changing the Class of an Instance
2091
2092You can change the class of an existing instance by invoking the
2093generic function @code{change-class} with two arguments: the instance
2094and the new class.
2095
2096@deffn generic change-class
2097@end deffn
2098
2099The default method for @code{change-class} decides how to implement the
2100change of class by looking at the slot definitions for the instance's
2101existing class and for the new class. If the new class has slots with
2102the same name as slots in the existing class, the values for those slots
2103are preserved. Slots that are present only in the existing class are
2104discarded. Slots that are present only in the new class are initialized
2105using the corresponding slot definition's init function (@pxref{Classes,,
2106slot-init-function}).
2107
2108@deffn {method} change-class (obj <object>) (new <class>)
2109Modify instance @var{obj} to make it an instance of class @var{new}.
2110
2111The value of each of @var{obj}'s slots is preserved only if a similarly named
2112slot exists in @var{new}; any other slot values are discarded.
2113
2114The slots in @var{new} that do not correspond to any of @var{obj}'s
2115pre-existing slots are initialized according to @var{new}'s slot definitions'
2116init functions.
2117@end deffn
2118
2119Customized change of class behaviour can be implemented by defining
2120@code{change-class} methods that are specialized either by the class
2121of the instances to be modified or by the metaclass of the new class.
2122
2123When a class is redefined (@pxref{Redefining a Class}), and the default
2124class redefinition behaviour is not overridden, GOOPS (eventually)
2125invokes the @code{change-class} generic function for each existing
2126instance of the redefined class.
2127
2128@node Introspection
2129@section Introspection
2130
2131@dfn{Introspection}, also known as @dfn{reflection}, is the name given
2132to the ability to obtain information dynamically about GOOPS metaobjects.
2133It is perhaps best illustrated by considering an object oriented language
2134that does not provide any introspection, namely C++.
2135
2136Nothing in C++ allows a running program to obtain answers to the following
2137types of question:
2138
2139@itemize @bullet
2140@item
2141What are the data members of this object or class?
2142
2143@item
2144What classes does this class inherit from?
2145
2146@item
2147Is this method call virtual or non-virtual?
2148
2149@item
2150If I invoke @code{Employee::adjustHoliday()}, what class contains the
2151@code{adjustHoliday()} method that will be applied?
2152@end itemize
2153
2154In C++, answers to such questions can only be determined by looking at
2155the source code, if you have access to it. GOOPS, on the other hand,
2156includes procedures that allow answers to these questions --- or their
2157GOOPS equivalents --- to be obtained dynamically, at run time.
2158
2159@menu
2160* Classes::
2161* Slots::
2162* Instances::
2163* Generic Functions::
2164* Generic Function Methods::
2165@end menu
2166
2167@node Classes
2168@subsection Classes
2169
2170@deffn {primitive procedure} class-name class
2171Return the name of class @var{class}.
2172This is the value of the @var{class} metaobject's @code{name} slot.
2173@end deffn
2174
2175@deffn {primitive procedure} class-direct-supers class
2176Return a list containing the direct superclasses of @var{class}.
2177This is the value of the @var{class} metaobject's
2178@code{direct-supers} slot.
2179@end deffn
2180
2181@deffn {primitive procedure} class-direct-slots class
2182Return a list containing the slot definitions of the direct slots of
2183@var{class}.
2184This is the value of the @var{class} metaobject's @code{direct-slots}
2185slot.
2186@end deffn
2187
2188@deffn {primitive procedure} class-direct-subclasses class
2189Return a list containing the direct subclasses of @var{class}.
2190This is the value of the @var{class} metaobject's
2191@code{direct-subclasses} slot.
2192@end deffn
2193
2194@deffn {primitive procedure} class-direct-methods class
2195Return a list of all the generic function methods that use @var{class}
2196as a formal parameter specializer.
2197This is the value of the @var{class} metaobject's @code{direct-methods}
2198slot.
2199@end deffn
2200
2201@deffn {primitive procedure} class-precedence-list class
2202Return the class precedence list for class @var{class} (@pxref{Class
2203precedence list}).
2204This is the value of the @var{class} metaobject's @code{cpl} slot.
2205@end deffn
2206
2207@deffn {primitive procedure} class-slots class
2208Return a list containing the slot definitions for all @var{class}'s slots,
2209including any slots that are inherited from superclasses.
2210This is the value of the @var{class} metaobject's @code{slots} slot.
2211@end deffn
2212
2213@deffn {primitive procedure} class-environment class
2214Return the value of @var{class}'s @code{environment} slot.
2215[ *fixme* I don't know what this value is used for. ]
2216@end deffn
2217
2218@deffn procedure class-subclasses class
2219Return a list of all subclasses of @var{class}.
2220@end deffn
2221
2222@deffn procedure class-methods class
2223Return a list of all methods that use @var{class} or a subclass of
2224@var{class} as one of its formal parameter specializers.
2225@end deffn
2226
2227@node Slots
2228@subsection Slots
2229
2230@deffn procedure class-slot-definition class slot-name
2231Return the slot definition for the slot named @var{slot-name} in class
2232@var{class}. @var{slot-name} should be a symbol.
2233@end deffn
2234
2235@deffn procedure slot-definition-name slot-def
2236Extract and return the slot name from @var{slot-def}.
2237@end deffn
2238
2239@deffn procedure slot-definition-options slot-def
2240Extract and return the slot options from @var{slot-def}.
2241@end deffn
2242
2243@deffn procedure slot-definition-allocation slot-def
2244Extract and return the slot allocation option from @var{slot-def}. This
2245is the value of the @code{#:allocation} keyword (@pxref{Slot Options,,
2246allocation}), or @code{#:instance} if the @code{#:allocation} keyword is
2247absent.
2248@end deffn
2249
2250@deffn procedure slot-definition-getter slot-def
2251Extract and return the slot getter option from @var{slot-def}. This is
2252the value of the @code{#:getter} keyword (@pxref{Slot Options,,
2253getter}), or @code{#f} if the @code{#:getter} keyword is absent.
2254@end deffn
2255
2256@deffn procedure slot-definition-setter slot-def
2257Extract and return the slot setter option from @var{slot-def}. This is
2258the value of the @code{#:setter} keyword (@pxref{Slot Options,,
2259setter}), or @code{#f} if the @code{#:setter} keyword is absent.
2260@end deffn
2261
2262@deffn procedure slot-definition-accessor slot-def
2263Extract and return the slot accessor option from @var{slot-def}. This
2264is the value of the @code{#:accessor} keyword (@pxref{Slot Options,,
2265accessor}), or @code{#f} if the @code{#:accessor} keyword is absent.
2266@end deffn
2267
2268@deffn procedure slot-definition-init-value slot-def
2269Extract and return the slot init-value option from @var{slot-def}. This
2270is the value of the @code{#:init-value} keyword (@pxref{Slot Options,,
2271init-value}), or the unbound value if the @code{#:init-value} keyword is
2272absent.
2273@end deffn
2274
2275@deffn procedure slot-definition-init-form slot-def
2276Extract and return the slot init-form option from @var{slot-def}. This
2277is the value of the @code{#:init-form} keyword (@pxref{Slot Options,,
2278init-form}), or the unbound value if the @code{#:init-form} keyword is
2279absent.
2280@end deffn
2281
2282@deffn procedure slot-definition-init-thunk slot-def
2283Extract and return the slot init-thunk option from @var{slot-def}. This
2284is the value of the @code{#:init-thunk} keyword (@pxref{Slot Options,,
2285init-thunk}), or @code{#f} if the @code{#:init-thunk} keyword is absent.
2286@end deffn
2287
2288@deffn procedure slot-definition-init-keyword slot-def
2289Extract and return the slot init-keyword option from @var{slot-def}.
2290This is the value of the @code{#:init-keyword} keyword (@pxref{Slot
2291Options,, init-keyword}), or @code{#f} if the @code{#:init-keyword}
2292keyword is absent.
2293@end deffn
2294
2295@deffn procedure slot-init-function class slot-name
2296Return the initialization function for the slot named @var{slot-name} in
2297class @var{class}. @var{slot-name} should be a symbol.
2298
2299The returned initialization function incorporates the effects of the
2300standard @code{#:init-thunk}, @code{#:init-form} and @code{#:init-value}
2301slot options. These initializations can be overridden by the
2302@code{#:init-keyword} slot option or by a specialized @code{initialize}
2303method, so, in general, the function returned by
2304@code{slot-init-function} may be irrelevant. For a fuller discussion,
2305see @ref{Slot Options,, init-value}.
2306@end deffn
2307
2308@node Instances
2309@subsection Instances
2310
2311@deffn {primitive procedure} class-of value
2312Return the GOOPS class of any Scheme @var{value}.
2313@end deffn
2314
2315@deffn {primitive procedure} instance? object
2316Return @code{#t} if @var{object} is any GOOPS instance, otherwise
2317@code{#f}.
2318@end deffn
2319
2320@deffn procedure is-a? object class
2321Return @code{#t} if @var{object} is an instance of @var{class} or one of
2322its subclasses.
2323@end deffn
2324
2325Implementation notes: @code{is-a?} uses @code{class-of} and
2326@code{class-precedence-list} to obtain the class precedence list for
2327@var{object}.
2328
2329@node Generic Functions
2330@subsection Generic Functions
2331
2332@deffn {primitive procedure} generic-function-name gf
2333Return the name of generic function @var{gf}.
2334@end deffn
2335
2336@deffn {primitive procedure} generic-function-methods gf
2337Return a list of the methods of generic function @var{gf}.
2338This is the value of the @var{gf} metaobject's @code{methods} slot.
2339@end deffn
2340
2341@node Generic Function Methods
2342@subsection Generic Function Methods
2343
2344@deffn {primitive procedure} method-generic-function method
2345Return the generic function that @var{method} belongs to.
2346This is the value of the @var{method} metaobject's
2347@code{generic-function} slot.
2348@end deffn
2349
2350@deffn {primitive procedure} method-specializers method
2351Return a list of @var{method}'s formal parameter specializers .
2352This is the value of the @var{method} metaobject's
2353@code{specializers} slot.
2354@end deffn
2355
2356@deffn {primitive procedure} method-procedure method
2357Return the procedure that implements @var{method}.
2358This is the value of the @var{method} metaobject's
2359@code{procedure} slot.
2360@end deffn
2361
2362@deffn generic method-source
2363@deffnx method method-source (m <method>)
2364Return an expression that prints to show the definition of method
2365@var{m}.
2366
2367@example
2368(define-generic cube)
2369
2370(define-method (cube (n <number>))
2371 (* n n n))
2372
2373(map method-source (generic-function-methods cube))
2374@result{}
2375((method ((n <number>)) (* n n n)))
2376@end example
2377@end deffn
2378
2379@node Miscellaneous Functions
2380@section Miscellaneous Functions
2381
2382@menu
2383* Administrative Functions::
2384* Error Handling::
2385* Object Comparisons::
2386* Cloning Objects::
2387* Write and Display::
2388@end menu
2389
2390@node Administrative Functions
2391@subsection Administration Functions
2392
2393This section describes administrative, non-technical GOOPS functions.
2394
2395@deffn primitive goops-version
2396Return the current GOOPS version as a string, for example ``0.2''.
2397@end deffn
2398
2399@node Error Handling
2400@subsection Error Handling
2401
2402The procedure @code{goops-error} is called to raise an appropriate error
2403by the default methods of the following generic functions:
2404
2405@itemize @bullet
2406@item
2407@code{slot-missing} (@pxref{Handling Slot Access Errors,, slot-missing})
2408
2409@item
2410@code{slot-unbound} (@pxref{Handling Slot Access Errors,, slot-unbound})
2411
2412@item
2413@code{no-method} (@pxref{Handling Invocation Errors,, no-method})
2414
2415@item
2416@code{no-applicable-method} (@pxref{Handling Invocation Errors,,
2417no-applicable-method})
2418
2419@item
2420@code{no-next-method} (@pxref{Handling Invocation Errors,,
2421no-next-method})
2422@end itemize
2423
2424If you customize these functions for particular classes or metaclasses,
2425you may still want to use @code{goops-error} to signal any error
2426conditions that you detect.
2427
2428@deffn procedure goops-error format-string . args
2429Raise an error with key @code{goops-error} and error message constructed
2430from @var{format-string} and @var{args}. Error message formatting is
2431as done by @code{scm-error}.
2432@end deffn
2433
2434@node Object Comparisons
2435@subsection Object Comparisons
2436
b3a9e3d5
MD
2437@deffn generic eqv?
2438@deffnx method eqv? ((x <top>) (y <top>))
2439@deffnx generic equal?
2440@deffnx method equal? ((x <top>) (y <top>))
2441@deffnx generic =
2442@deffnx method = ((x <number>) (y <number>))
a0e07ba4
NJ
2443Generic functions and default (unspecialized) methods for comparing two
2444GOOPS objects.
2445
b3a9e3d5
MD
2446The default method for @code{eqv?} returns @code{#t} for all values
2447that are equal in the sense defined by R5RS and the Guile reference
2448manual, otherwise @code{#f}. The default method for @code{equal?}
2449returns @code{#t} or @code{#f} in the sense defined by R5RS and the
2450Guile reference manual. If no such comparison is defined,
2451@code{equal?} returns the result of a call to @code{eqv?}. The
2452default method for = returns @code{#t} if @var{x} and @var{y} are
2453numerically equal, otherwise @code{#f}.
2454
2455Application class authors may wish to define specialized methods for
2456@code{eqv?}, @code{equal?} and @code{=} that compare instances of the
2457same class for equality in whatever sense is useful to the
2458application. Such methods will only be called if the arguments have
2459the same class and the result of the comparison isn't defined by R5RS
2460and the Guile reference manual.
a0e07ba4
NJ
2461@end deffn
2462
2463@node Cloning Objects
2464@subsection Cloning Objects
2465
2466@deffn generic shallow-clone
2467@deffnx method shallow-clone (self <object>)
2468Return a ``shallow'' clone of @var{self}. The default method makes a
2469shallow clone by allocating a new instance and copying slot values from
2470self to the new instance. Each slot value is copied either as an
2471immediate value or by reference.
2472@end deffn
2473
2474@deffn generic deep-clone
2475@deffnx method deep-clone (self <object>)
2476Return a ``deep'' clone of @var{self}. The default method makes a deep
2477clone by allocating a new instance and copying or cloning slot values
2478from self to the new instance. If a slot value is an instance
2479(satisfies @code{instance?}), it is cloned by calling @code{deep-clone}
2480on that value. Other slot values are copied either as immediate values
2481or by reference.
2482@end deffn
2483
2484@node Write and Display
2485@subsection Write and Display
2486
2487@deffn {primitive generic} write object port
2488@deffnx {primitive generic} display object port
2489When GOOPS is loaded, @code{write} and @code{display} become generic
2490functions with special methods for printing
2491
2492@itemize @bullet
2493@item
2494objects - instances of the class @code{<object>}
2495
2496@item
2497foreign objects - instances of the class @code{<foreign-object>}
2498
2499@item
2500classes - instances of the class @code{<class>}
2501
2502@item
2503generic functions - instances of the class @code{<generic>}
2504
2505@item
2506methods - instances of the class @code{<method>}.
2507@end itemize
2508
2509@code{write} and @code{display} print non-GOOPS values in the same way
2510as the Guile primitive @code{write} and @code{display} functions.
2511@end deffn
2512
2513@node MOP Specification, Tutorial, Reference Manual, Top
2514@chapter MOP Specification
2515
2516For an introduction to metaobjects and the metaobject protocol,
2517see @ref{Metaobjects and the Metaobject Protocol}.
2518
2519The aim of the MOP specification in this chapter is to specify all the
2520customizable generic function invocations that can be made by the standard
2521GOOPS syntax, procedures and methods, and to explain the protocol for
2522customizing such invocations.
2523
2524A generic function invocation is customizable if the types of the arguments
2525to which it is applied are not all determined by the lexical context in
2526which the invocation appears. For example,
2527
2528@itemize @bullet
2529@item
2530the @code{(initialize @var{instance} @var{initargs})} invocation in the
2531default @code{make-instance} method is customizable, because the type of the
2532@code{@var{instance}} argument is determined by the class that was passed to
2533@code{make-instance}.
2534
2535@item
2536the @code{(make <generic> #:name ',name)} invocation in @code{define-generic}
2537is not customizable, because all of its arguments have lexically determined
2538types.
2539@end itemize
2540
2541When using this rule to decide whether a given generic function invocation
2542is customizable, we ignore arguments that are expected to be handled in
2543method definitions as a single ``rest'' list argument.
2544
2545For each customizable generic function invocation, the @dfn{invocation
2546protocol} is explained by specifying
2547
2548@itemize @bullet
2549@item
2550what, conceptually, the applied method is intended to do
2551
2552@item
2553what assumptions, if any, the caller makes about the applied method's side
2554effects
2555
2556@item
2557what the caller expects to get as the applied method's return value.
2558@end itemize
2559
2560@menu
2561* Class Definition::
2562* Instance Creation::
2563* Class Redefinition::
2564* Method Definition::
2565* Generic Function Invocation::
2566@end menu
2567
2568@node Class Definition
2569@section Class Definition
2570
2571@code{define-class} (syntax)
2572
2573@itemize @bullet
2574@item
2575@code{class} (syntax)
2576
2577@itemize @bullet
2578@item
2579@code{make-class} (procedure)
2580
2581@itemize @bullet
2582@item
2583@code{make @var{metaclass} @dots{}} (generic)
2584
2585@var{metaclass} is the metaclass of the class being defined, either
2586taken from the @code{#:metaclass} class option or computed by
2587@code{ensure-metaclass}. The applied method must create and return the
2588fully initialized class metaobject for the new class definition.
2589@end itemize
2590
2591@end itemize
2592
2593@item
2594@code{class-redefinition @var{old-class} @var{new-class}} (generic)
2595
2596@code{define-class} calls @code{class-redefinition} if the variable
2597specified by its first argument already held a GOOPS class definition.
2598@var{old-class} and @var{new-class} are the old and new class metaobjects.
2599The applied method should perform whatever is necessary to handle the
2600redefinition, and should return the class metaobject that is to be bound
2601to @code{define-class}'s variable. The default class redefinition
2602protocol is described in @ref{Class Redefinition}.
2603@end itemize
2604
2605The @code{(make @var{metaclass} @dots{})} invocation above will create
2606an class metaobject with metaclass @var{metaclass}. By default, this
2607metaobject will be initialized by the @code{initialize} method that is
2608specialized for instances of type @code{<class>}.
2609
2610@code{initialize <class> @var{initargs}} (method)
2611
2612@itemize @bullet
2613@item
2614@code{compute-cpl @var{class}} (generic)
2615
2616The applied method should compute and return the class precedence list
2617for @var{class} as a list of class metaobjects. When @code{compute-cpl}
2618is called, the following @var{class} metaobject slots have all been
2619initialized: @code{name}, @code{direct-supers}, @code{direct-slots},
2620@code{direct-subclasses} (empty), @code{direct-methods}. The value
2621returned by @code{compute-cpl} will be stored in the @code{cpl} slot.
2622
2623@item
2624@code{compute-slots @var{class}} (generic)
2625
2626The applied method should compute and return the slots (union of direct
2627and inherited) for @var{class} as a list of slot definitions. When
2628@code{compute-slots} is called, all the @var{class} metaobject slots
2629mentioned for @code{compute-cpl} have been initialized, plus the
2630following: @code{cpl}, @code{redefined} (@code{#f}), @code{environment}.
2631The value returned by @code{compute-slots} will be stored in the
2632@code{slots} slot.
2633
2634@item
2635@code{compute-get-n-set @var{class} @var{slot-def}} (generic)
2636
2637@code{initialize} calls @code{compute-get-n-set} for each slot computed
2638by @code{compute-slots}. The applied method should compute and return a
2639pair of closures that, respectively, get and set the value of the specified
2640slot. The get closure should have arity 1 and expect a single argument
2641that is the instance whose slot value is to be retrieved. The set closure
2642should have arity 2 and expect two arguments, where the first argument is
2643the instance whose slot value is to be set and the second argument is the
2644new value for that slot. The closures should be returned in a two element
2645list: @code{(list @var{get} @var{set})}.
2646
2647The closures returned by @code{compute-get-n-set} are stored as part of
2648the value of the @var{class} metaobject's @code{getters-n-setters} slot.
2649Specifically, the value of this slot is a list with the same number of
2650elements as there are slots in the class, and each element looks either like
2651
2652@example
2653@code{(@var{slot-name-symbol} @var{init-function} . @var{index})}
2654@end example
2655
2656or like
2657
2658@example
2659@code{(@var{slot-name-symbol} @var{init-function} @var{get} @var{set})}
2660@end example
2661
2662Where the get and set closures are replaced by @var{index}, the slot is
2663an instance slot and @var{index} is the slot's index in the underlying
2664structure: GOOPS knows how to get and set the value of such slots and so
2665does not need specially constructed get and set closures. Otherwise,
2666@var{get} and @var{set} are the closures returned by @code{compute-get-n-set}.
2667
2668The structure of the @code{getters-n-setters} slot value is important when
2669understanding the next customizable generic functions that @code{initialize}
2670calls@dots{}
2671
2672@item
2673@code{compute-getter-method @var{class} @var{gns}} (generic)
2674
2675@code{initialize} calls @code{compute-getter-method} for each of the class's
2676slots (as determined by @code{compute-slots}) that includes a
2677@code{#:getter} or @code{#:accessor} slot option. @var{gns} is the
2678element of the @var{class} metaobject's @code{getters-n-setters} slot that
2679specifies how the slot in question is referenced and set, as described
2680above under @code{compute-get-n-set}. The applied method should create
2681and return a method that is specialized for instances of type @var{class}
2682and uses the get closure to retrieve the slot's value. [ *fixme Need
2683to insert something here about checking that the value is not unbound. ]
2684@code{initialize} uses @code{add-method!} to add the returned method to
2685the generic function named by the slot definition's @code{#:getter} or
2686@code{#:accessor} option.
2687
2688@item
2689@code{compute-setter-method @var{class} @var{gns}} (generic)
2690
2691@code{compute-setter-method} is invoked with the same arguments as
2692@code{compute-getter-method}, for each of the class's slots that includes
2693a @code{#:setter} or @code{#:accessor} slot option. The applied method
2694should create and return a method that is specialized for instances of
2695type @var{class} and uses the set closure to set the slot's value.
2696@code{initialize} then uses @code{add-method!} to add the returned method
2697to the generic function named by the slot definition's @code{#:setter}
2698or @code{#:accessor} option.
2699@end itemize
2700
2701@node Instance Creation
2702@section Instance Creation
2703
2704@code{make <class> . @var{initargs}} (method)
2705
2706@itemize @bullet
2707@item
2708@code{allocate-instance @var{class} @var{initargs}} (generic)
2709
2710The applied @code{allocate-instance} method should allocate storage for
2711a new instance of class @var{class} and return the uninitialized instance.
2712
2713@item
2714@code{initialize @var{instance} @var{initargs}} (generic)
2715
2716@var{instance} is the uninitialized instance returned by
2717@code{allocate-instance}. The applied method should initialize the new
2718instance in whatever sense is appropriate for its class. The method's
2719return value is ignored.
2720@end itemize
2721
2722@node Class Redefinition
2723@section Class Redefinition
2724
2725The default @code{class-redefinition} method, specialized for classes
2726with the default metaclass @code{<class>}, has the following internal
2727protocol.
2728
a0e07ba4
NJ
2729@code{class-redefinition @var{(old <class>)} @var{(new <class>)}}
2730(method)
2731
2732@itemize @bullet
2733@item
2734@code{remove-class-accessors! @var{old}} (generic)
2735
2736@item
2737@code{update-direct-method! @var{method} @var{old} @var{new}} (generic)
2738
2739@item
2740@code{update-direct-subclass! @var{subclass} @var{old} @var{new}} (generic)
2741@end itemize
2742
da901526
MD
2743This protocol cleans up things that the definition of the old class
2744once changed and modifies things to work with the new class.
2745
2746The default @code{remove-class-accessors!} method removes the
2747accessor methods of the old class from all classes which they
2748specialize.
2749
2750The default @code{update-direct-method!} method substitutes the new
2751class for the old in all methods specialized to the old class.
2752
a0e07ba4 2753The default @code{update-direct-subclass!} method invokes
da901526
MD
2754@code{class-redefinition} recursively to handle the redefinition of
2755subclasses.
a0e07ba4
NJ
2756
2757When a class is redefined, any existing instance of the redefined class
2758will be modified for the new class definition before the next time that
2759any of the instance's slot is referenced or set. GOOPS modifies each
da901526 2760instance by calling the generic function @code{change-class}.
a0e07ba4
NJ
2761
2762The default @code{change-class} method copies slot values from the old
ddee39a1 2763to the modified instance, and initializes new slots, as described in
a0e07ba4
NJ
2764@ref{Changing the Class of an Instance}. After doing so, it makes a
2765generic function invocation that can be used to customize the instance
2766update algorithm.
2767
2768@code{change-class @var{(old-instance <object>)} @var{(new <class>)}} (method)
2769
2770@itemize @bullet
2771@item
2772@code{update-instance-for-different-class @var{old-instance} @var{new-instance}} (generic)
2773
2774@code{change-class} invokes @code{update-instance-for-different-class}
2775as the last thing that it does before returning. The applied method can
2776make any further adjustments to @var{new-instance} that are required to
2777complete or modify the change of class. The return value from the
2778applied method is ignored.
2779
2780The default @code{update-instance-for-different-class} method does
2781nothing.
2782@end itemize
2783
2784@node Method Definition
2785@section Method Definition
2786
2787@code{define-method} (syntax)
2788
2789@itemize @bullet
2790@item
2791@code{add-method! @var{target} @var{method}} (generic)
2792
2793@code{define-method} invokes the @code{add-method!} generic function to
2794handle adding the new method to a variety of possible targets. GOOPS
2795includes methods to handle @var{target} as
2796
2797@itemize @bullet
2798@item
2799a generic function (the most common case)
2800
2801@item
2802a procedure
2803
2804@item
2805a primitive generic (@pxref{Extending Guiles Primitives})
2806@end itemize
2807
2808By defining further methods for @code{add-method!}, you can
2809theoretically handle adding methods to further types of target.
2810@end itemize
2811
2812@node Generic Function Invocation
2813@section Generic Function Invocation
2814
2815[ *fixme* Description required here. ]
2816
2817@code{apply-generic}
2818
2819@itemize @bullet
2820@item
2821@code{no-method}
2822
2823@item
2824@code{compute-applicable-methods}
2825
2826@item
2827@code{sort-applicable-methods}
2828
2829@item
2830@code{apply-methods}
2831
2832@item
2833@code{no-applicable-method}
2834@end itemize
2835
2836@code{sort-applicable-methods}
2837
2838@itemize @bullet
2839@item
2840@code{method-more-specific?}
2841@end itemize
2842
2843@code{apply-methods}
2844
2845@itemize @bullet
2846@item
2847@code{apply-method}
2848@end itemize
2849
2850@code{next-method}
2851
2852@itemize @bullet
2853@item
2854@code{no-next-method}
2855@end itemize
2856
8cb6d96d 2857@node Tutorial, Concept Index, MOP Specification, Top
a0e07ba4
NJ
2858@chapter Tutorial
2859@include goops-tutorial.texi
2860
8cb6d96d 2861@node Concept Index, Function and Variable Index, Tutorial, Top
a0e07ba4
NJ
2862@unnumberedsec Concept Index
2863
2864@printindex cp
2865
2866@node Function and Variable Index, , Concept Index, Top
2867@unnumberedsec Function and Variable Index
2868
2869@printindex fn
2870
2871@summarycontents
2872@contents
2873@bye