static `this`

via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 8 05:13:16 PST 2015


On Sunday, 8 February 2015 at 01:57:55 UTC, Meta wrote:
> On Sunday, 8 February 2015 at 00:31:42 UTC, Mike wrote:
>> Is `this` overloaded to mean "this class" in a static context 
>> or is `this` only valid in a non-static context.  Please 
>> clarify, and if it's a bug, help me understand so I can make 
>> an accurate and actionable bug report.
>
> `this` should only be valid in a non-static context, as far as 
> I know. That's pretty much what static means: "there is no 
> `this`".

The following is a useful idiom for static factory methods:

     struct S {
         this(int x) {
         }

         static auto make(int x) {
             return typeof(this)(x);
         }
     }


More information about the Digitalmars-d mailing list