opDispatch doesn't play nice with inheritance

Carl Sturtivant sturtivant at gmail.com
Thu Nov 15 17:14:21 UTC 2018


//Consider this:
import std.stdio;

void main() {
     X obj = new Y;
     writeln( obj._f() );
}

class Proxy {
     X x;
     this(X x) { this.x = x; }
     string _f() { return "Proxy._f called"; }
}

class X {
     auto opDispatch(string f, Args...)(Args args) {
         Proxy p = new Proxy(this);
         return mixin("p."~f~"(args)");
     }
}

class Y : X {
     string _f() { return "Y._f called"; }
}
//

Presumably the presence of obj._f()in main causes the compilation 
of a function _f() in the class X, yet the function _f() in Y 
that inherits it merely shadows it; the keyword override cannot 
be used for this function.

opDispatch is special in that it allows for functions to be added 
to a class or struct when undefined overtly but used elsewhere 
but it seems those functions sadly are final.

Can anything useful be done to remedy the situation?




More information about the Digitalmars-d-learn mailing list