Next: , Previous: , Up: Methods and Generic Functions   [Contents][Index]


8.6.4 Next-method

When you call a generic function, with a particular set of arguments, GOOPS builds a list of all the methods that are applicable to those arguments and orders them by how closely the method definitions match the actual argument types. It then calls the method at the top of this list. If the selected method’s code wants to call on to the next method in this list, it can do so by using next-method.

(define-method (test (a <integer>)) (cons 'integer (next-method)))
(define-method (test (a <number>))  (cons 'number  (next-method)))
(define-method (test a)             (list 'top))

With these definitions,

(test 1)   ⇒ (integer number top)
(test 1.0) ⇒ (number top)
(test #t)  ⇒ (top)

next-method can be called as just (next-method). The arguments for the next method call are then implicit, and the same as for the original method call.

If you want to call on to a method with the same name but with a different set of arguments (as you might with overloaded methods in C++, for example), you can pass custom arguments to next-method:

(define-method (test (a <number>) min max)
  (if (and (>= a min) (<= a max))
      (display "Number is in range\n"))
  (next-method a))

(test 2 1 10)
-|
Number is in range
⇒
(integer number top)