Automatic method overriding in sub-classes

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Wed Oct 28 17:11:04 PDT 2015


On Wednesday, 28 October 2015 at 21:48:36 UTC, bitwise wrote:
> There is nothing spooky going on here. The declaration would be 
> clearly annotated with auto override, or whatever keyword was 
> chosen. If you were inheriting from a class, and didn't like 
> its auto overrides, you could manually override the functions 
> in your own class to stop it.
>
> There are several discussion on these boards about using 
> druntime's RTInfo(T) achieve similar goals to what(I think) 
> we're trying to do here. This feature we're talking about 
> nicely dodges the issues presented by RTInfo(T), because it's 
> effects are localized to a single class hierarchy, instead of 
> trying to affect all classes universally. I'm thinking there 
> may be a better solution somewhere in between though.
>
> Having the compiler turn something with template parameters 
> into a virtual function is a bit odd, and I don't think it will 
> be obvious to everyone. I think the following would suit my 
> needs, and may be a better solution:
>
> class Base {
>     static this(this This)() { }
> }
>
> The compiler would instantiate that static constructor for 
> every sub class. From there, any other relevant templates could 
> be instantiated, and at runtime, this static ctor could stash 
> any relevant information in a collection under the key 
> 'typeid(This)'. Finally, 'Base' could implement a function that 
> looks up the class's info by typeid(this).
>
> I believe this solution is a great deal simpler, and less 
> invasive.
>
>     Bit

I could not think of a way to implement the auto override 
functionality with your proposal.
But while thinking about it came up with another way that is 
similar and very close to being doable now.

      class Base { this(this T)(){ writeln(T.stringof); } }
      class Child : Base {}

That fails to compile, but the error is in Child for not calling 
the parent constructor.
Change it to:

      class Base { this(this T)(){ writeln(T.stringof); } }
      class Child : Base { this(){ super(); } }

And it works and the Base this(this T) gets the right T depending 
on if you do new Base or new Child.
With that you could easily implement the auto override 
functionality with a single function pointer in the base class.
The only thing to make it perfect would be to have the compiler 
properly recognize this(this T)(){} and instantiate it for the 
Child class.

I think this plus your static version could do a lot and be 
pretty useful while still being pretty simple.


More information about the Digitalmars-d mailing list