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