static `this`

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 9 10:05:07 PST 2015


On 2/8/15 5:54 PM, Mike wrote:
> On Sunday, 8 February 2015 at 13:13:18 UTC, Marc Schütz wrote:
>> 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);
>>         }
>>     }
>
> Yes, there are a few cases where `this` seems to be allowed in a static
> context, but is that by accident, or by design?

typeof(this) is supposed to work in a static context. I can't find the 
spec part that describes this, but it definitely is not a bug.

Everything else is a bug.

-Steve


More information about the Digitalmars-d mailing list