Overriding of inherited class static methods?

monkyyy crazymonkyyy at gmail.com
Tue Jul 21 14:25:09 UTC 2026


On Tuesday, 21 July 2026 at 13:11:37 UTC, Steven Schveighoffer 
wrote:
> On Tuesday, 21 July 2026 at 10:51:30 UTC, Denis F wrote:
>> Hi!
>>
>> Can anyone remind me why D doesn't provides overriding of 
>> static methods?
>> Is there some fundamental problem with this?
>>
>> I think it would be great in itself and also intuitive.
>
> How does it work? Overriding of a function works because you 
> have a class instance. A static function has no instance.
>
> -Steve

```d
import std;

template innate(T,alias data,discrim...){
     T innate=data;
}
template innateempty(T,discrim...){
     T innateempty;
}
interface base{
     int foo(int);
     void __hassetupfoo__();
     TypeInfo mytypeinfo();
}
mixin template setupfoo(alias foo){
     TypeInfo mytypeinfo()=>typeid(typeof(this));
     static this(){
         alias store=innateempty!(int 
function(int)[TypeInfo],"foo");
     //store[typeid(new typeof(this)).classinfo]=&foo; // 
".classinfo applied to an interface gives the information for the 
interface, not the class it might be an instance of." WHY?
	store[(new typeof(this)).mytypeinfo]=&foo;
     }
     void __hassetupfoo__(){}
}
class A:base{
     int foo(int i)=>i+2;
     static int foo_(int i)=>i+2;
     mixin setupfoo!(foo_);
}
class B:base{
     int foo(int i)=>assert(0);
     static int foo_(int i)=>i*2;
     mixin setupfoo!(foo_);
}
int callfoo(base b,int i){
     alias store=innateempty!(int function(int)[TypeInfo],"foo");
     //return store[typeid(b).classinfo](i);
     return store[b.mytypeinfo](i);
}
unittest{
     base bar;
     bar=new A();
     bar.callfoo(3).writeln;
     bar=new B();
     bar.callfoo(3).writeln;
}
```

you just need to store it somewhere


More information about the Digitalmars-d mailing list