Next: , Previous: , Up: Methods   [Contents][Index]

5.2 Method Syntax

The following syntactic form greatly simplifies the definition of methods, and of adding them to generic procedures.

Syntax: define-method generic-procedure lambda-list body …

Defines a method of generic-procedure. Lambda-list is like the parameter list of a lambda special form, except that the required parameters may have associated specializers. A parameter with an associated specializer is written as a list of two elements: the first element is the parameter’s name, and the second element is an expression that evaluates to a class.

Lambda-list must contain at least one required parameter, and at least one required parameter must be specialized.

A define-method special form expands into the following:

(add-method generic-procedure
  (make-method (list specializer …)
    (lambda (call-next-method . stripped-lambda-list)
      body …)))

where stripped-lambda-list is lambda-list with the specialized parameters replaced by their names, and the specializers are the corresponding expressions from the specialized parameters. If necessary, the specializers are interspersed with references to <object> in order to make them occur in the correct position in the sequence.

For example,

(define-method add ((x <integer>) (y <rational>)) ...)

expands into

(add-method add
  (make-method (list <integer> <rational>)
    (lambda (call-next-method x y) ...)))

Note that the list of specializers passed to make-method will correspond to the required parameters of the method; the specializer corresponding to a non-specialized required parameter is <object>.

Further note that, within the body of a define-method special form, the free variable call-next-method is bound to a “call-next-method” procedure (see make-chained-method for details). If the define-method body refers to this variable, the defined method is a chained method, otherwise it is an ordinary method.

Next: Chained Methods, Previous: Method Datatype, Up: Methods   [Contents][Index]