Automatic method overriding in sub-classes

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Wed Oct 28 18:52:15 PDT 2015


On Thursday, 29 October 2015 at 01:14:35 UTC, Tofu Ninja wrote:
> On Thursday, 29 October 2015 at 00:11:06 UTC, Tofu Ninja wrote:
>> [...]
>
> Actually never mind, what I just said was basically auto 
> override for this() so its not really any different. And it is 
> kinda limited with some problems.

My argument though, is that a virtual function isn't needed. I'm 
not sure if this is sufficient for your use cases, but if I could 
do the this, I would have everything I need:

struct MyInfo { string name; }
static MyInfo[string] stuff;

class Base {
     static this(this This)() {
         stuff[fullyQualifiedName!This] = MyInfo(This.stringof);
     }
}

class Derived : Base {
     // static this called for Derived as well
  }

void main(string[] args)
{
     writeln(stuff["main.Base"].name);
     writeln(stuff["main.Derived"].name);
}

I think this would be a lot easier to understand than the virtual 
function approach. There would be no need to explain the 
complicated semantics of what happens when the definition is 
missing(can't inherit from the class) or how manually overriding 
these auto override functions stops the overriding, etc... 
template static this would simply be a static constructor that 
gets called for all subclasses, and nothing more.

When I thought about the virtual function approach, my intention 
was to have the virtual function return information about the 
subclass for the purpose of serialization or inspection. To be 
honest, I can't think of any other functionality I would want to 
specialize in this way.

     Bit



More information about the Digitalmars-d mailing list