Consider this simple example
A)-----------------------------------------
struct StaticRegister {
    static private uint _value;
    @property static uint value() { return _value; }
    @property static void value(uint v) { _value = v; }
}

void main(string[] s) {
    StaticRegister = 1;
    assert(StaticRegister == 1);
}
-------------------------------------------

This gives two errors for each line in `main` (exactly what I expected).

  test.d(8): Error: StaticRegister is not an lvalue
test.d(9): Error: incompatible types for ((StaticRegister) == (1)):
  cannot use '==' with types


However, if I modify the example by adding an `alias this` ...
B)-----------------------------------------
struct StaticRegister {
    static private uint _value;
    @property static uint value() { return _value; }
    @property static void value(uint v) { _value = v; }

    alias value this;
}

void main(string[] s) {
    StaticRegister = 1;
    assert(StaticRegister == 1);
}
-------------------------------------------

... the assignment error is eliminated, but the read is not.

test.d(11): Error: incompatible types for ((StaticRegister) == (1)):
  cannot use '==' with types

I argue that either both errors should be eliminated, or neither should be eliminated. One could also argue that some variation of the following should be required...
   * static alias value this;
   * alias static value this;
   * alias value static this;
  ... to distinguish it from non-static `this`

Now, in the example below, `this` is referring to the type itself in a static context
C)-------------------------------------------
import std.stdio;

struct StaticRegister {
    static string GetType() { return typeof(this).stringof; }
}

void main(string[] s) {
    writeln(StaticRegister.GetType());
}
-------------------------------------------

So, it follows that the example below should work... and it does
D)-------------------------------------------
struct StaticRegister {
    static private uint _value = 0;
    @property static uint value() { return _value; }
    @property static void value(uint v) { _value= v; }

    static uint GetValue() {
        return this.value;
    }
}

void main(string[] s) {
    assert(StaticRegister.GetValue() == 0);
}
-------------------------------------------

So, why does `alias this` in a static context (See example B above) only half-work? Bug? If not, what's the design rationale?

Thanks,
Mike

Reply via email to