Overriding of inherited class static methods?

ShadoLight ettienne.gilbert at gmail.com
Thu Jul 23 14:30:39 UTC 2026


On Thursday, 23 July 2026 at 10:28:40 UTC, Denis F wrote:
> On Wednesday, 22 July 2026 at 14:03:53 UTC, ShadoLight wrote:
>
>> If you then propose to add static functions to the vtable to 
>> allow them to be overridden - that would then in turn cause 
>> static functions not to be callable using the class name 
>> (```D.foo(..)``` above) as the vtable is associated with the 
>> instance of the class (which is passed via the implicit 
>> ```this``` pointer),
>
> Oblivous, this should be a special compile-time "vtbl" as 
> proposed by monkyyy above

monkyyys proposal does not give you a "special compile-time 
"vtbl"". And monkyyy's solution neither gives you 'overriding' 
behavior of a static method in a derived class. He merely showed, 
using some template wizardry, that he can 'redirect' the call to 
the virtual method that implements the abstract method declared 
in an *interface*, to  a static method in the same class.

This does not give you what you originally asked namely "Can 
anyone remind me why D doesn't provides overriding of static 
methods?"

- There is no static method in monkyyy's ```base``` class. In 
fact, it is not even a class, it is an interface.
- The hierarchy can only be one deep eg. everything needs to be 
derived from ```base```. For example, class B cannot be derived 
from class A. This will be extremely limiting, even discounting 
the ton of template boilerplate this requires.
- You have to call ```callfoo(..)``` on the instance, and not 
```foo(..)```, yet...
- ... you still need a virtual ```foo(..)``` implemented in the 
class to satisfy the ```foo``` declaration in the interface.
- The static method cannot be the same name (```foo``` here) as 
the interface method being "overridden".

But, **most of all**, if you accidently (using monkyyy's code as 
an example) call...
```D
bar.foo(3).writeln;
```
... instead of...
```D
bar.callfoo(3).writeln;
```
... the virtual ```foo``` will be called, and not the static 
```foo_```.

This code is not only confusing to read and overly complex for 
what it delivers - it will be bug-prone as hell since a lot of 
people will call ```foo``` on the instance, instead of 
```callfoo```. This is no implementable general solution.

This "overriding of static methods" idea is such a non-starter 
that I assumed you meant "overloading", rather than "overriding" 
- which you in fact confirmed. And, yet, since then you have kept 
pushing this conversation back towards "overriding" ... it is a 
bad idea and I've tried to show you some of the implications. If 
you have some ideas how this can be a good idea then show how it 
will work, etc.




More information about the Digitalmars-d mailing list