Dynamic templated virtuals - I still often want them

Adam D. Ruppe destructionator at gmail.com
Thu Jul 23 14:49:10 UTC 2020


On Thursday, 23 July 2020 at 08:46:54 UTC, Arafel wrote:
> Specifically, I want to be able to transform Compile-time 
> introspection into run-time instrospection:

That's a possible use case of this too, depending on how it is 
implemented. You might be able to make an interface method that 
includes a static initializer.

interface Reflectable {
     virtual void callMethod(this This)(string name) {
             // normal CT reflection based impl
     }
}

Then any

class A : Reflectable {}

will automatically implement the callMethod interface thanks to 
the virtual template.


Also MIGHT be possible to auto-register:

     virtual template reflect(this This) {
         // and maybe for registering a factory function...
         shared static this() { /* register */ }
     }


but that's a lot iffier because a long-form template like that 
may have multiple members and muddy up the interface. Still worth 
considering though.


In the past, proposals on this area have been along the lines of 
the parent class/interface defining a mixin template that the 
compiler automatically mixes into its child classes. That would 
definitely enable the auto-registration, among other things. 
That's actually a strict superset of my new idea and I'd be happy 
with it too, just this simpler one might have advantages in its 
simplicity; it certainly feels natural to write.


More information about the Digitalmars-d mailing list