Dynamic templated virtuals - I still often want them

Max Samukha maxsamukha at gmail.com
Thu Jul 23 15:00:59 UTC 2020


On Thursday, 23 July 2020 at 14:41:06 UTC, Adam D. Ruppe wrote:
> On Thursday, 23 July 2020 at 03:50:03 UTC, Manu wrote:
>> That's clever, and I wish I had have thought of it before!!
>
> Yeah, in the past my thought was so much on `mixin` this 
> particular thing never came to mind. But last night, I was 
> trying to goof around with no-runtime classes again (actually 
> very easy to do basics nowadays, I'll probably blog about this 
> monday) and wanted to experiment with dynamic casts and this 
> came up... and I was just like "omg if the virtual slot got a 
> template instance it would rock".
>
> And thus it came to mind.

FWIW, a more general way to initialize an object with static type 
data would be (currently it fails for indirectly derived classes 
due to a compiler bug):

class Base {
     this(this This)() {
         _serialize = function(Base base) {
             // base can be safely reinterpreted into This here if 
needed

             return This.stringof; // just for example
         };
     }

     final string serialize() {
         return _serialize(this);
     }

     private string function(Base) _serialize;
}

class Derived: Base {
}

void main() {
     Base b = new Derived;
     assert(b.serialize() == "Derived");
}

Also, there seems to be no good reason "this T" should not work 
for any class member, including class static constructors.


More information about the Digitalmars-d mailing list