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