Access derived type in baseclass static function template

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 2 06:51:01 PDT 2017


On 8/2/17 9:11 AM, Timoses wrote:
> On Wednesday, 2 August 2017 at 12:49:12 UTC, Steven Schveighoffer wrote: 
> Thanks for the reply!
> 
> Not sure I understand correctly, though.
> 
> interface I {}
> class A : I {}
> 
> void test(T)(T t) if(is(T: I))
> { writeln(T.type.stringof); }
> 
> void main()
> {
>      A.test;
> }
> 
> throws:
>    Error: no property 'test' for type 'app.A'
> 
> which it of course does... test(A) makes no sense either.
> I need to call it statically and want to know the derived class type in 
> the base class (or interface) function template.
> 
> Is there a trick in your answer? : P

Sorry, I didn't read your original post thoroughly, you need an instance 
to make this work properly.

A a;
a.test; // should work

What you are looking for is virtual static methods, and D doesn't have 
those. I don't know if there's a way to make it work with existing features.

However, your original code has potential as an enhancement request, as 
the type is known at compile-time and could certainly be resolved to 
pass in as the `this` template parameter.

-Steve


More information about the Digitalmars-d-learn mailing list