Node:Basic Method Definition, Next:, Up:Adding Methods to Generic Functions



3.6.1 Basic Method Definition

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 define-method will include a call to define-generic. If generic is (setter generic-with-setter), where generic-with-setter is a variable which is not yet bound to a generic-with-setter object, the expansion will include a call to define-accessor.

Each parameter must be either a symbol or a two-element list (symbol class). The symbols refer to variables in the body that will be bound to the parameters supplied by the caller when calling this method. The classes, if present, specify the possible combinations of parameters to which this method can be applied.

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.