Common Lisp the Language, 2nd Edition

Next: Standard Method Combination Up: Method Selection and Previous: Method Selection and

### 28.1.7.1. Determining the Effective Method

The effective method for a set of arguments is determined by the following three-step procedure:

1. Select the applicable methods.

2. Sort the applicable methods by precedence order, putting the most specific method first.

3. Apply method combination to the sorted list of applicable methods, producing the effective method.

Selecting the Applicable Methods. This step is described in section 28.1.6.2.

Sorting the Applicable Methods by Precedence Order. To compare the precedence of two methods, their parameter specializers are examined in order. The default examination order is from left to right, but an alternative order may be specified by the `:argument-precedence-order` option to `defgeneric` or to any of the other forms that specify generic function options.

The corresponding parameter specializers from each method are compared. When a pair of parameter specializers are equal, the next pair are compared for equality. If all corresponding parameter specializers are equal, the two methods must have different qualifiers; in this case, either method can be selected to precede the other.

If some corresponding parameter specializers are not equal, the first pair of parameter specializers that are not equal determines the precedence. If both parameter specializers are classes, the more specific of the two methods is the method whose parameter specializer appears earlier in the class precedence list of the corresponding argument. Because of the way in which the set of applicable methods is chosen, the parameter specializers are guaranteed to be present in the class precedence list of the class of the argument.

If just one parameter specializer is `(`eql` object)`, the method with that parameter specializer precedes the other method. If both parameter specializers are `eql` forms, the specializers must be the same (otherwise the two methods would not both have been applicable to this argument).

The resulting list of applicable methods has the most specific method first and the least specific method last.

Applying Method Combination to the Sorted List of Applicable Methods. In the simple case-if standard method combination is used and all applicable methods are primary methods-the effective method is the most specific method. That method can call the next most specific method by using the function `call-next-method`. The method that `call-next-method` will call is referred to as the next method. The predicate `next-method-p` tests whether a next method exists. If `call-next-method` is called and there is no next most specific method, the generic function `no-next-method` is invoked.

In general, the effective method is some combination of the applicable methods. It is defined by a Lisp form that contains calls to some or all of the applicable methods, returns the value or values that will be returned as the value or values of the generic function, and optionally makes some of the methods accessible by means of `call-next-method`. This Lisp form is the body of the effective method; it is augmented with an appropriate lambda-list to make it a function.

The role of each method in the effective method is determined by its method qualifiers and the specificity of the method. A qualifier serves to mark a method, and the meaning of a qualifier is determined by the way that these marks are used by this step of the procedure. If an applicable method has an unrecognized qualifier, this step signals an error and does not include that method in the effective method.

When standard method combination is used together with qualified methods, the effective method is produced as described in section 28.1.7.2.

Another type of method combination can be specified by using the `:method-combination` option of `defgeneric` or of any of the other forms that specify generic function options. In this way this step of the procedure can be customized.

New types of method combination can be defined by using the `define-method-combination` macro.

The meta-object level also offers a mechanism for defining new types of method combination. The generic function `compute-effective-method` receives as arguments the generic function, the method combination object, and the sorted list of applicable methods. It returns the Lisp form that defines the effective method. A method for `compute-effective-method` can be defined directly by using `defmethod` or indirectly by using `define-method-combination`. A method combination object is an object that encapsulates the method combination type and options specified by the `:method-combination` option to forms that specify generic function options.

Implementation note: In the simplest implementation, the generic function would compute the effective method each time it was called. In practice, this will be too inefficient for some implementations. Instead, these implementations might employ a variety of optimizations of the three-step procedure. Some illustrative examples of such optimizations are the following:

- Use a hash table keyed by the class of the arguments to store the effective method.

- Compile the effective method and save the resulting compiled function in a table.

- Recognize the Lisp form as an instance of a pattern of control structure and substitute a closure that implements that structure.

- Examine the parameter specializers of all methods for the generic function and enumerate all possible effective methods. Combine the effective methods, together with code to select from among them, into a single function and compile that function. Call that function whenever the generic function is called.

Next: Standard Method Combination Up: Method Selection and Previous: Method Selection and