Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this compiles in a static context #18946

Open
dlangBugzillaToGithub opened this issue Feb 12, 2015 · 1 comment
Open

this compiles in a static context #18946

dlangBugzillaToGithub opened this issue Feb 12, 2015 · 1 comment
Labels

Comments

@dlangBugzillaToGithub
Copy link

Mike Franklin reported this on 2015-02-12T00:11:30Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=14170

CC List

  • Nick Treleaven (@ntrel)
  • Simen Kjaeraas

Description

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.
@dlangBugzillaToGithub
Copy link
Author

simen.kjaras commented on 2018-05-14T13:25:51Z

(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.

@thewilsonator thewilsonator added the Feature:alias alias and alias this label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants