Dynamic templated virtuals - I still often want them

Arafel er.krali at gmail.com
Wed Jul 29 09:36:43 UTC 2020


On 29/7/20 10:01, Alexandru Ermicioi wrote:
> Well it does. How do you plan to handle the case where Y library is 
> already compiled (separately, or available as .lib with .di interface)? 
> Vtable should already be defined, which means that upon loading of lib, 
> main app needs to patch vtable with missing template specializations.
> 
> Best regards,
> Alexandru.

I think the trick here is that they wouldn't actually be templates in 
the usual sense. So:

```
class Base {
     virtual void fun(this This)() { /* body */ }
}
```

would be lowered to:

```
class Base {
     void fun() {
         alias This = Base;
         /* body */
     }
}
```

The code for `fun` would still be available in the .di file (as with any 
other template), so when you then later do:

```
class Derived : Base { }
```

it would be rewritten / lowered as:

```
class Derived : Base {
     override void fun() {
         alias This = Derived;
         /* body */
     }
}
```

that could be then perfectly interact with all other compilation units.

I'm happy to be corrected if I understood it wrong, but I think it could 
work this way without any side-effect (i.e. you can try the lowered 
version now and it works):

```
class Base {
     string serialize() {
         alias This = Base;
	return This.stringof; // just for example
     }
}

class Derived : Base {
     override string serialize() {
         alias This = Derived;
	return This.stringof; // just for example
     }
}

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


More information about the Digitalmars-d mailing list