by David A. Moon

February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.

Previous page Table of Contents Next page

Invocation of a function is a four-step process:

Selection | Selects the applicable subset of all the methods of the function. A method is applicable if it accepts the number of arguments that were supplied in the function invocation and if each argument is a member of the type declared for the corresponding parameter of the method. When a method has keyword parameters, the method is applicable only if each argument in a keyword position matches a keyword parameter and each argument in a value position is a member of the type declared for the corresponding keyword parameter. |

Ordering | Sorts the applicable methods according to generality. The details of this are explained below. |

Dispatch | Passes control to the least general applicable method, or signals an error if there are no applicable methods or there is not a unique least general applicable method. |

Execution | Performs the behavior dictated by the dispatched method, which can include delegating to other applicable methods. |

The rules for method ordering are as follows:

Define the < relation on types to be true if and only if the two types are different and the left-hand type is a subtype of the right-hand type. The left-hand type is said to be less general than the right-hand type. This relation is transitive and is anti-commutative when the arguments are distinct types.

In more detail:

Every type is < anything, except anything itself.

Class C1 < class C2 if and only if C1 inherits from C2 directly or indirectly.

Protocol P1 < protocol P2 if and only if P1 inherits from P2 directly or indirectly.

Class C < protocol P if and only if C inherits from P directly or indirectly.

Range R1 < range R2 if every integer in R1 is also in R2 but not vice versa.

Range R < class integer and anything that class integer is <.

Type T < union U if and only if T is not a union and T < some member of U. This assumes that unions of adjacent or overlapping ranges are canonicalized into a single range.

Union U < type T if and only if each member of U is < T.

Define the < relation on methods by comparison of the type restrictions of corresponding parameters. Match required and optional parameters by position and keyword parameters by name. Match rest parameters only to rest parameters. If there is no corresponding parameter use anything as its type restriction. The left-hand method is < the right-hand method if and only if there is at least one corresponding pair of parameters where the left-hand method's parameter's type restriction is < the right-hand method's parameter's type restriction and there is no corresponding pair of parameters where the right-hand method's parameter's type restriction is < the left-hand method's parameter's type restriction. The left-hand method is said to be less general than the right-hand method. This relation is anti-commutative but not transitive.

A set of applicable methods has a unique least general method M if and only if M is < every other method in the set and no other method in the set has that property. M is the method executed.

Previous page Table of Contents Next page