D:在子类中覆盖opDispatch

Gus*_*lad 3 inheritance overriding d opdispatch

有没有办法在子类中覆盖opDispatch?我真正想做的是传递一个以超类作为静态类型的变量,但是将对opDispatch的调用重定向到它的子类型(动态类型).

基本上,我希望此代码打印"Sub"而不是"Super".

import std.stdio;

class Super
{
     void opDispatch(string m)()
     {
         writeln("Super");
     }
}

class Sub : Super
{
    override void opDispatch(string m)()
    {
        writeln("Sub");
    }
}

void main()
{   
    Super s = new Sub();
    s.callingOpDispatch; // Writes "Super" instead of "Sub"
}
Run Code Online (Sandbox Code Playgroud)

我傻眼了,因为我无法通过使用抽象方法强制编译器查找方法覆盖(D不允许抽象模板化方法).

PS:有人可以创建标签opDispatch吗?(在我看来这对D会有好处吗?)

Mic*_*ich 6

成员模板函数不能是虚拟的,因此无法覆盖.

http://dlang.org/function.html#virtual-functions

opDispatch是模板化函数.这两个电话是相同的:

s.callingOpDispatch();
s.opDispatch!("callingOpDispatch")()
Run Code Online (Sandbox Code Playgroud)