[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.5 Generic function and method

Defining methods

Macro: define-generic name &keyword class

Creates a generic function and bind it to name.

You don't usually need to use this, since the define-method macro implicitly creates a generic function if it doesn't exist yet.

You can pass a subclass of <generic> to the class keyword argument so that the created generic function will be the instance of the passed class, instead of the default <generic> class. It is useful when you defined a subclass of <generic> to customize generic function application behavior.

Macro: define-method name specs body

Defines a method whose name is name. If there's already a generic function object globally bound to name, the created method is added to the generic function. If name is unbound, or bound to an object except a generic function, then a new generic function is created, bound to name, then a new method is added to it.

Specs specifies the arguments and their types for this method. It's like the argument list of lambda form, except you can specify the type of each argument.

 
specs : ( arg … )
      | ( arg … . symbol )
      | symbol

arg   : ( symbol class )
      | symbol

Class specifies the class that the argument has to belong to. If arg is just a symbol, it is equivalent to (arg <top>). You can't specify the type for the "rest" argument, for it is always bound to a list.

The list of classes of the argument list is called method specializer list, based on which the generic function will select appropriate methods(s). Here are some examples of specs and the corresponding specializer list:

 
specs:        ((self <myclass>) (index <integer>) value)
specializers: (<myclas> <integer> <top>)

specs:        ((self <myclass>) obj . options)
specializers: (<myclas> <top> . <top>)

specs:        (obj (attr <string>))
specializers: (<top> <string>)

specs:        args
specializers: <top>

If you define a method on name whose specializer list matches with one in the generic function's methods, then the existing method is replaced by the newly defined one.

Applying generic function

When a generic function is applied, first it selects methods whose specializer list matches the given arguments. For example, suppose a generic function foo has three methods, whose specializer lists are (<string> <top>), (<string> <string>), and (<top> <top>), respectively. When foo is applied like (foo "abc" 3), the first and the third method will be selected.

Then the selected methods are sorted from the most specific method to the least specific method. It is calculated as follows:

Once methods are sorted, the body of the first method is called with the actual argument.

Within the method body, a special local variable next-method is bound implicitly.

Next method: next-method
Next method: next-method args …

This variable is bound within a method body to a special object that encapsulates the next method in the sorted method list.

Calling without arguments invokes the next method with the same arguments as this method is called with. Passing args … explicitly invokes the next method with the passed arguments.

If next-method is called in the least specific method, i.e. there's no "next method", an error is signalled.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on October, 7 2008 using texi2html 1.78.