https://issues.dlang.org/show_bug.cgi?id=14170
Issue ID: 14170
Summary: `this` compiles in a static context
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: DMD
Assignee: [email protected]
Reporter: [email protected]
This code compiles and executes:
-----------------------------------
import std.stdio;
struct StaticRegister {
static private uint _value;
@property static uint value() { return _value; }
@property static void value(uint v) { _value = v; }
static alias value this; // (1)
static void test() {
writeln(this.stringof); // (2)
writeln(typeof(this).stringof); // (3)
writeln(this.value); // (4)
}
}
void main(string[] s) {
// works due to `alias value this`
StaticRegister = 1;
StaticRegister.test();
}
-----------------------------------
I suspect (1), (2), (3), and (4) should all generate compiler errors. `static
alias value this` and `alias value this` seem to be semantically the same
thing, but I can't be sure.
I attempted to understand the meaning of `this` in a static context on the
forum...
http://forum.dlang.org/post/[email protected]
http://forum.dlang.org/post/[email protected]
...but I was unable to elicit a definitive answer.
One member of the community believes (3) is valid, and the rest are not. If
that is the case, it needs to be documented in the language specification at
http://dlang.org/expression.html#this. At the moment the spec is silent on the
subject.
Issue #380 is a D1 bug that was fixed to specifically allow `this` in a static
context, but I'm not sure if it applies to D2.
--