The less-familiar parts of Lisp for beginners — call-next-method

Our next function for newcomers to Lisp is call-next-method.  This may or may not appear in an introduction to Lisp’s object-oriented system, CLOS.  You might want to review this earlier post for an introduction, including an example of the use of call-next-method.

The call-next-method function causes the program to call another method in the combination of the class.  Recall that, in Lisp, we have generic functions, which you can think of as being like the non-member functions of C++.  There is no equivalent to the member functions of C++.  Still, as these generic functions can be specialized on different classes in an inheritance hierarchy, we have polymorphism based on object type.  This function, in its simple usage, allows you to invoke functions in parent classes.  Where, in C++, you explicitly supply the name of the parent class whose method you want to invoke, in Lisp, by default, it searches out the next less specific method and calls that.

When a class does not inherit from multiple base classes, it is simple to use the default method combination rules.  It is also possible, of course, to define custom method combination rules when necessary, but I’m only going to deal with the defaults here.  You can see the default rules at this page in the CLHS.  Basically, it works as follows:  if there are one or more :around methods, the most specific one is executed, instead of calling the normal methods.  Typically, an :around method calls call-next-method to invoke the primary method, because otherwise the primary method is skipped entirely.  However, if there is a less specific :around method, this is called instead, and so on until there are no remaining :around methods, in which case call-next-method invokes, in order:

  • All of the :before methods in order, from most to least specific.
  • The most specific primary method (which can, itself, call call-next-method to invoke less specific primary methods).
  • All of the :after methods in order, from least to most specific (note, this is opposite to the order of the :before methods).

If call-next-method is called in a primary method, it invokes the next less specific primary method (if any).  The call-next-method function cannot be called from within a :before or :after method.

Check the documentation for details on how the presence or absence of arguments to call-next-method affects the parameters passed to the invoked method.

If the default method combination method is unsuitable, for instance if you want a particular class to call a method specialized on the grandparent class and skip over the method specialized on the parent class, that can be done by modifying the default with define-method-combination, which we will discuss later.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.