Idiomatic way to call base method in generic code

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 24 16:32:50 PDT 2015


import std.stdio, std.conv, std.traits;

class Base
{
     int x;
     override string toString() const
     {
         return x.to!string;
     }
}

class Derived : Base
{
     int y;
     override string toString() const
     {
         return y.to!string;
     }
}

void callMethod(T, alias Method)(const T thiz)
{
     thiz.fun();
}

void callBaseMethod(SubClass, alias Method)(const SubClass thiz)
{
     alias FirstSuperClass = BaseClassesTuple!(SubClass)[0];
     thiz.FirstSuperClass.Method();
}

void main()
{
     Derived d = new Derived();
     d.y = 15; d.x = 13;

     // 1) callMethod!(Derived, Derived.toString)(d);
     // 2) callBaseMethod!(Derived, Derived.toString)(d);
     // 3) writeln(d.Base.toString());

}


I know I can call a base implementation of a method like in 3), 
but I need to do it generically like in 2). Does anybody now how 
I can achieve this?
Additionally is there a way to make 1) work without using string 
mixins?


More information about the Digitalmars-d-learn mailing list