Multi methods
You are here :

Sub nodes

Recent changes
Site plan


Single dispatch

You all know of single dispatch that enables OO programmers to use late binding (aka polymorphism). This is a dispatch that is done solely on the instance object. For example :

class A {public sub f() {}}
class B extends A {public sub f() {}}
class C extends A {public sub f() {}}
A a1 = new B();
A a2 = new C();
a1.f(); // will call at runtime B.f(), because a1 is really of type B
a2.f(); // will call at runtime C.f(), because a2 is really of type C

In Nosica, this come at no cost as the resolution of late binding is done at compilation time. That is, a phase that we call "Global analysis" is performed at compil time to resolve all polymorphism call and resolve them (when possible) at compil time. If they cannot be fully resolved some more tests will be performed at runtime.

Multiple dispatch

This is the same kind of idea, except that the dispatch is done using all arguments of the method.

class A {
  public sub f(A a) {}
class B extends A {
  public sub f(B b) {}
class C extends A {
 public sub f(C c) {}
A a1 = new B();
a1.f(new B());  // will really call B.f(B)
A a2 = new C();
a2.f(new C());  // will really call C.f(C)

Nosica is invariant on arguments, that is, when implementing an interface you have to implement exactly the signature of your interface for the argument.

interface A {
  sub f(A a);
  A g();
class AImpl implements A {
  public sub f(A a) {}      // correct, right signature (invariance)
  AImpl g() {}              // correct, covariance is allowed on result type
class AImpl2 implements A {
  public sub f(AImpl2 a) {} // incorrect, wrong signature (covariance)
  public A g() {}           // correct too

So you cannot declare a multi method until you have properly implemented the proper interface. As single dispatch, multiple dispatch come at no cost for most of cases. Resolution is done at compil time for most cases, so this kind of "multiple late binding" become "multiple early binding".

More informations

See this thread on forum.


Propulsed by Wiclear