Overloading free functions & run-time dispatch based on parameter types

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 5 07:23:53 PST 2016


Does the following help?

import std.algorithm.comparison : castSwitch;
import std.stdio;

class A { }
class B : A { }
class C : A { }

auto foo_impl(B b) {
     writeln("called foo(B)");
}
auto foo_impl(C c) {
     writeln("called foo(C)");
}

auto foo(A a) {
     return a.castSwitch!(
         (B b) => foo_impl(b),
         (C c) => foo_impl(c),
     );
}

void main()
{
    B b = new B();
    A a = b;

    foo(a); // called foo(B)
}

With a bit of template magic, you can make it DRY, e.g.

alias foo = buildSwitch!foo_impl;


More information about the Digitalmars-d-learn mailing list