LEVIATHAN vd52e79c4 · d52e79c486

Expressions

Method dispatch

An instance method call runs the override of the receiver's actual class, whatever static type names the receiver.

since 0.1.0-alpha.1linuxwindowswasm

Description

An unqualified instance method call runs the method of the receiver's runtime class. This holds for receivers typed as an interface and for receivers typed as a class. If Dog extends Animal and overrides speak(), then speak() on any Animal-typed variable, field, parameter or method reference that holds a Dog runs Dog's version, not the version of the type the variable was declared with.

Operators dispatch the same way: an operator that a subclass overrides runs the override when the left operand holds that subclass, whether the program writes the operator (a + 1, a += 1, a != b) or a construct calls it (a match value or range arm). See lang.operator-overloading. Constructor selection and calls to static or namespace functions are resolved by the names written in the program and do not dispatch on the runtime class. A base-qualified call (this.Base::m()) is resolved statically to Base's own method when the bare call would be unambiguous: this is how an override calls the method it overrides. Where the bare call would be ambiguous between distinct paths, this.Base::m() selects Base's path and dispatches on the receiver's runtime class to the most-derived body on that path. It becomes a direct call under the same rule as an unqualified call (see lang.distinct).

The compiler may turn a call into a direct call when it can prove that no class in the program overrides the method below the receiver's static type. That is purely a speed optimization: it never changes which method runs.

The override of the actual class runs

use io::console;

class Animal {
    string speak() => "...";
}

class Dog : Animal {
    string speak() => "Woof";
}

class Cat : Animal {
    string speak(string s) => "Meow ${s}";
}

string callSpeak(Animal a) => a.speak();

console.writeln(callSpeak(Dog()));
console.writeln(callSpeak(Animal()));

Animal a = Dog();
console.writeln(a.speak());
console.writeln(Dog().Animal::speak());

Array<Animal> zoo = [Animal(), Dog()];
for (Animal z in zoo) {
    console.writeln(z.speak());
}

console.writeln(Cat().speak());
console.writeln(Cat().speak("x"));
Woof
...
Woof
...
...
Woof
...
Meow x

Rules

  • The overload is chosen at compile time from the static types; dispatch then runs the runtime class's override of that overload. It does not use the parameter types to choose between overloads at run time, so an overload that only the runtime class (or another of its bases) declares never answers the call.
  • An overridden method may share its name and parameter count with another overload, declared in the base, in the subclass or on another base. The call runs the override of the overload the static types chose, and a call that chooses the other overload runs that one's override, or the base's body when nothing overrides it.
  • Interface dispatch, path dispatch and method references follow the same rules.
  • A base's type arguments can make two of its overloads one signature: under G<string>, put(T x) and put(string x) are both put(string). A class that inherits both and overrides neither keeps both bodies, so a G<string>-typed call runs the same body on it as on a G<string>. A call typed as that class can run the other body: the class holds one method for the two, the one declared later, while a G<string>-typed call picks the one declared first, so GD d = GD(); d.put("x") and G<string> g = d; g.put("x") can differ. One method cannot stand for both, so two cases are compile errors, for operators as well as methods. A method written in the class that matches both, as in class GD : G<string> { string put(string x) ... }, is reported at its declaration: method 'put' of 'GD' overrides two overloads of 'G<string>' (put(T) and put(string)), which have one signature there. The second case is a class whose type arguments bring the two together after a generic base overrode one of them, class GD<U> : G<U> { string put(string x) ... } and class GDD : GD<string> { }. It is reported at the base clause: class 'GDD' merges two overloads of 'G<string>' (put(T) and put(string)) into one method, but they run different bodies. Give the overloads distinct names or parameter counts.
  • A call that is dispatched at run time, such as a method call on a value of an erased generic type T, runs only a method whose number of parameters equals the number of arguments (or a field of that name holding a function). A method is never run with arguments missing. Every type bound to T is checked for such a method at compile time (lang.generics): a binding whose type has the name only with other parameter counts is a compile error at the binding, type 'Counter' bound to 'T' of 'bumpNothing' has no method 'bump' taking 0 arguments. Such a call has no static overload to dispatch on: where the bound type has two methods of that name and count, the first declared runs, so the binding is a compile error when the use needs another one (... has two methods 'm' taking 1 argument, m(string) and m(int): the use needs m(int), but it runs the first of them, m(string)), and so is an operator on T in the same position. The engines still raise cannot resolve call target '<name>' if a call finds no method, but no checked program reaches that.

Examples

A qualified call in a class that holds a distinct method on two paths runs the override on that path, through a receiver typed as the combining class as well:

Path dispatch through a base-typed receiver

use io::console;

class Race { distinct string cry() => "..."; }
class Job  { distinct string cry() => "..."; }
class Elf     : Race { string cry() => "For the grove!"; }
class Fighter : Job  { string cry() => "Hold the line!"; }
class Unit : Race, Job {
    string shout() => this.Race::cry() + " " + this.Job::cry();
}
class Archer : Unit, Elf, Fighter { }

Unit u = Archer();
console.writeln(u.shout());
console.writeln(Unit().shout());
For the grove! Hold the line!
... ...

A call on an erased generic value is dispatched at run time, to the override of the runtime class:

Dispatch through an erased generic value

use io::console;

class Counter {
    int bump(int n) => n + 1;
}
class Fast : Counter {
    int bump(int n) => n + 10;
}

int bumpOnce<T>(T x) => x.bump(1);

Counter c = Fast();
console.writeln(bumpOnce(c));
console.writeln(bumpOnce(Counter()));
11
2

An override beside an overload with the same parameter count runs only for the overload it overrides:

An override beside a same-count overload

use io::console;

class Animal {
    string speak(string s) => "a-str";
    string speak(int n) => "a-int";
}
class Dog : Animal {
    string speak(int n) => "d-int";   // overrides speak(int)
}
Animal a = Dog();
console.writeln(a.speak(5));
console.writeln(a.speak("x"));
console.writeln(Animal().speak(5));
d-int
a-str
a-int

Notes

A subclass method with the same name but a different parameter count, like Cat.speak(string) above, is a separate overload, not an override: Cat().speak() still reaches the version inherited from Animal.

See also

  • Array — An ordered sequence of values of one type, Array<T>, with value semantics.