Dynamic templated virtuals - I still often want them

Tove tove at fransson.se
Thu Jul 23 09:51:36 UTC 2020


On Thursday, 23 July 2020 at 08:46:54 UTC, Arafel wrote:
> On 23/7/20 4:38, Adam D. Ruppe wrote:
>> That does NOT work today. For one, of course, D has no 
>> `virtual` keyword, but if you left that out, it would compile, 
>> but fail the assert because the static type of `b` passed to 
>> the template This is actually still `Base`.
>
> This does work today, you just don't need the `virtual` keyword:
>
> ```
> class Base {
>      string serialize(this This)() {
>            return This.stringof; // just for example
>      }
> }
>
> class Derived : Base {
>
> }
>
> void main() {
>     Base b = new Derived();
>     assert(b.serialize() == "Derived");
> }
> ```
>
> https://run.dlang.io/is/vp1koK
>

It compiles but doesn't work, in your link you compiled with 
"-c", so the runtime assert was never evalutated.





More information about the Digitalmars-d mailing list