<div dir="ltr"><div dir="ltr"></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 23, 2020 at 12:40 PM Adam D. Ruppe via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">You might know about D's template this parameters, which adapt to <br>
the static type of a `this` reference on a call, or with the <br>
curiously recurring template pattern, which passes a derived <br>
class to the base class so the base class can inspect the derived <br>
class.<br>
<br>
Both of these are useful at times, but neither quite do what I <br>
have in mind here.<br>
<br>
Imagine this:<br>
<br>
---<br>
class Base {<br>
      virtual string serialize(this This)() {<br>
            return This.stringof; // just for example<br>
      }<br>
}<br>
<br>
class Derived : Base {<br>
<br>
}<br>
<br>
void main() {<br>
     Base b = new Derived();<br>
     assert(b.serialize() == "Derived");<br>
}<br>
---<br>
<br>
That does NOT work today. For one, of course, D has no `virtual` <br>
keyword, but if you left that out, it would compile, but fail the <br>
assert because the static type of `b` passed to the template This <br>
is actually still `Base`.<br>
<br>
But imagine if `serialize` actually got a virtual table entry, <br>
just like if it wasn't a template at all, but each child class <br>
automatically got an instance of it made for itself.<br>
<br>
So it would be as if I wrote<br>
<br>
class Base {<br>
      string serialize() {<br>
            return Base.stringof;<br>
      }<br>
}<br>
<br>
class Derived : Base {<br>
      override string serialize() {<br>
            return Derived.stringof;<br>
      }<br>
}<br>
<br>
by hand. Of course, it is possible to do this kind of thing with <br>
mixin templates or the CRTP, but in both cases, the Derived class <br>
must actually write it in the child class, and if you don't <br>
there's no way to really tell; it will still compile and just use <br>
the base class implementation. Which might be OK but it isn't <br>
perfect.<br>
<br>
It would just be cool if the base class template was <br>
automatically instantiated again for the child class, while still <br>
working like a normal virtual call.<br></blockquote><div><br></div><div>That's clever, and I wish I had have thought of it before!!</div><div>This pattern would have helped me in multiple scenarios I've encountered in the past where I had to deploy awkward and ugly mixin tricks.</div></div></div>