static `this`

Mike via Digitalmars-d digitalmars-d at puremagic.com
Sun Feb 8 14:54:13 PST 2015


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?

If by accident, then then we have a bug that, unfortunately, 
people may be using as a feature.  If by design, then there's 
still a bug because it doesn't work consistently.

The grammar specification [1] is silent about the semantics of 
`this` in a static context, and the examples show usage only in a 
non-static context.

So, my qustion still remains:  Does `this` mean "this 
class/struct" in a static context, or does `this` have no 
semantic meaning in a static context?

Mike

[1] - http://dlang.org/expression.html#this


More information about the Digitalmars-d mailing list