Node:Basic Method Definition, Next:Method Definition Internals, Up:Adding Methods to Generic Functions
To add a method to a generic function, use the define-method
form.
define-method (generic parameter ...) . body | syntax |
Define a method for the generic function or accessor generic with
parameters parameters and body body.
generic is a generic function. If generic is a variable
which is not yet bound to a generic function object, the expansion of
Each parameter must be either a symbol or a two-element list
body is the body of the method definition. |
define-method
expressions look a little like normal Scheme
procedure definitions of the form
(define (name formals ...) . body)
The most important difference is that each formal parameter, apart from the
possible "rest" argument, can be qualified by a class name:
formal
becomes (formal class)
. The
meaning of this qualification is that the method being defined
will only be applicable in a particular generic function invocation if
the corresponding argument is an instance of class
(or one of
its subclasses). If more than one of the formal parameters is qualified
in this way, then the method will only be applicable if each of the
corresponding arguments is an instance of its respective qualifying class.
Note that unqualified formal parameters act as though they are qualified
by the class <top>
, which GOOPS uses to mean the superclass of
all valid Scheme types, including both primitive types and GOOPS classes.
For example, if a generic function method is defined with
parameters ((s1 <square>) (n <number>))
, that method is
only applicable to invocations of its generic function that have two
parameters where the first parameter is an instance of the
<square>
class and the second parameter is a number.
If a generic function is invoked with a combination of parameters for which there is no applicable method, GOOPS raises an error. For more about invocation error handling, and generic function invocation in general, see Invoking Generic Functions.