Access derived type in baseclass static function template

Timoses via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 3 07:15:38 PDT 2017


On Wednesday, 2 August 2017 at 15:38:12 UTC, Steven Schveighoffer 
wrote:
> On 8/2/17 11:06 AM, Timoses wrote:
>> On Wednesday, 2 August 2017 at 13:51:01 UTC, Steven 
>> Schveighoffer wrote:
>>> 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.
>> 
>> I guess the `this` is really misleading it being a static 
>> method.
>> I suppose 
>> https://issues.dlang.org/buglist.cgi?bug_severity=enhancement&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&product=D&query_format=report-table&y_axis_field=bug_severity would be the place for an enhancement request?
>> Possibly related:
>> https://issues.dlang.org/show_bug.cgi?id=14191
>
> Yes, that's exactly it.
>
> Note that typeof(this) has special meaning for static methods:
>
> class C
> {
>    static typeof(this) foo() { return new typeof(this); }
> }
>
> So there is precedence for the meaning of `this` in a static 
> context.
>
> -Steve

Oh, just now get it after creating the issue ^^. Makes sense. I 
kind of connected the "this" to existing instances because the 
DMD compiler often complains about something like "need this 
for..." when calling something from a static method that... 
well... needs "this" or instantiation..
E.g.:

class A
{
     static void a()
     {
         b();
     }
     void b();
}

will throw
   Error: need 'this' for 'b' of type 'void()'


More information about the Digitalmars-d-learn mailing list