Multiple Dispatch in practice

bearophile bearophileHUGS at lycos.com
Tue Oct 21 19:01:15 PDT 2008


On the Lambda the Ultimate forum I have just found a nice article about Multiple Dispatch in Practice:
http://homepages.mcs.vuw.ac.nz/~alex/files/MuscheviciPotaninTemperoNobleOOPSLA2008.pdf

I have seen that this topic was also discussed here:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=56018


This is normal D1 code that shows a manual implementation of double dispatch:

import std.stdio: writefln;

/// ...
template IsInstance(Class) {
    static assert(is(Class == class), "IsInstance: Class must be a class");

    bool IsInstance(TyObj)(TyObj obj) {
        return cast(Class)obj !is null;
    }
}

interface Vehicle {
    void collide(Vehicle v);
}

class Car : Vehicle {
    void collide(Vehicle v) {
        if (IsInstance!(Car)(v))
            writefln("Car hits car");
        else if (IsInstance!(Bike)(v))
            writefln("Car hits bike");
        else
            throw new Exception("missing case: should not happen");
    }
}

class Bike : Vehicle {
    void collide(Vehicle v) {
        if (IsInstance!(Car)(v))
            writefln("Bike hits car");
        else if (IsInstance!(Bike)(v))
            writefln("Bike hits bike");
        else
            throw new Exception("missing case: should not happen");
    }
}

void main() {
    Vehicle car = new Car();
    Vehicle bike = new Bike();

    car.collide(bike);  // Car hits bike
    car.collide(car);   // Car hits car
    bike.collide(car);  // Bike hits car
    bike.collide(bike); // Bike hits bike
}



With built-in multiple (double) dispatch the two classes become something similar to this:

class Car : Vehicle {
    void collide(Car c) {
        writefln("Car hits car");
    }
    void collide(Bike b) {
        writefln("Car hits bike");
    }
}

class Bike : Vehicle {
    void collide(Car c) {
        writefln("Bike hits car");
    }
    void collide(Bike b) {
        writefln("Bike hits bike");
    }
}

Bye,
bearophile



More information about the Digitalmars-d mailing list