6.2 Method dispatch for multimethods
A method is specialized on the required parameters that have explicit types. The type of the required parameter is called that parameter's specializer. A multimethod is a method that specializes more than one of its parameters. The methods that we defined in Section 6.1 specialize two required parameters, and therefore are multimethods.
Comparison with C++ and Java: Neither C++ nor Java supports multimethods. In both languages, method dispatch is based on the first argument of virtual functions. |
The method dispatch considers all the required parameters, and sorts the applicable methods by specificity as follows: For each required parameter, construct a separate list of the applicable methods, sorted from most specific to least specific for that parameter. Then, combine the separate sorted lists into an overall list of methods, sorted by specificity. In the overall method ordering, a method is more specific than another if it satisfies two constraints:
1. The method is no less specific than the other method for all required parameters. (The two methods might have the same types for some parameters.)
2. The method is more specific than the other method for some required parameter.
One method might be more specific than another for one parameter, but less specific for another parameter. These two methods are ambiguous in specificity and cannot be ordered. If the method-dispatch procedure cannot find any method that is more specific than all other methods, Dylan signals an error.
Table 6.1 shows the applicable methods for various arguments to +. If two methods are applicable, we number the more specific method 1, and the less specific method 2.
We call + on two instances of <time-offset>:
? *minus-2-hours* + *plus-15-20-45*;
{instance of <time-offset>}
When both arguments are instances of <time-offset>, the first row of the table applies. Two methods are applicable. The method on <time-offset>, <time-offset> is more specific than the method on <time>, <time>. The parameter specializers of the method on <time-offset>, <time-offset> are subtypes of the parameter specializers of the method on <time>, <time>. That is, for the first parameter, <time-offset> is a subtype of <time>; for the second parameter, <time-offset> is a subtype of <time>.




