April 2002
Multimethods and Function Overloading
Multimethods are a quite intuitive concept. Multimethods are concerned with how to route messages, or function calls, to functions. This routing can happen in the context of function overloading, function specialization, class specialization or multiple inheritance. We will inspect each of these contexts and show what should happen.
Function Overloading
When more than one function is declared with the same name, it is called overloading. The domain of a function call will dictate which of the declared functions will be executed. Suppose Teacher and Administrator are disjoint classes, here is an example of overloaded function f:
void f(Teacher p);
void f(Administrator p);
The KMAL has a richer potential function domain than most other OO systems because KMAL uses keyword parameters. The keywords are another type of domain specification. Therefore
FUNCTION f(p AS Person);
FUNCTION f(q AS Person);
are overloaded functions with completely disjoint domains.
Function Specialization
When the domains of the declared functions overlap there could be ambiguity over which function to execute. A protocol must be defined that determines which, if any, of the functions should be called. KMAL, as with most other OO langauges, use covariant function specialization rules.
Class Specialization
Class methods are specialized using function specialization rules. The only difference here is the scope of those rules; namely the scope of the class definition and the scope of it's inheriting children.
Multiple Inheritance
When methods of the same name and domain are found in more than one ancestor of a class then what method should be executed? There are three possible solutions to this ambiguity:
- The parent classes could be prioritized, like in CLOS or Python. This does remove the ambiguity over which function to execute but can lead to unintended consequences. The methods can have behavior that is hard to find in code.
- All methods from each parent class can be run. Many times multiple inheritance is used to combine independent classes, and running the class methods independently could be smart to do. This is an interesting scenario that could very well be the best solution. I am not sure though.
- The KMAL chooses the safest route: If there exists an ambiguous function call somewhere in the system, then the KMAL will indicate what method domains need clarification. This error is often a compile-time error, so it is only a little more work for the programmer. The benefit, however, is a guarantee that the programmer has considered, and clarified, the ambiguous methods..
Conclusion
Most OO systems implement the multimethod dispatch correctly. except that the dispatch is based on the static parameter types. This static multimethod dispatch is traditionally called "function overloading". CLOS implements dynamic multimethod dispatch.
April 2002: First writing