[Issue 14170] New: `this` compiles in a static context

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Feb 11 16:11:30 PST 2015


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: nobody at puremagic.com
          Reporter: slavo5150 at yahoo.com

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.org
http://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.

--


More information about the Digitalmars-d-bugs mailing list