You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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/xcnwuneclebuyqcjbkwu@forum.dlang.orghttp://forum.dlang.org/post/ubatudbwrakkwzulpewp@forum.dlang.org
...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.
The text was updated successfully, but these errors were encountered:
(1) is a case of DMD ignoring a redundant keyword - 'static' has no effect there, so it's just ignored. It's been mentioned that this is due to static blocks in aggregates, but I'm unsure of the exact details:
struct S {
static { // or just static:
// lots of declarations
alias value this;
}
}
(3) is explicitly mentioned in https://dlang.org/spec/declaration.html#typeof:
Special cases:
typeof(this) will generate the type of what this would be in a non-static member function, even if not in a member function.
(2) and (4) are bug 6579. Possibly a bit more than that, since they don't have an actual instance to work with, but that's the root.
Mike Franklin reported this on 2015-02-12T00:11:30Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=14170
CC List
Description
The text was updated successfully, but these errors were encountered: