Summary: alias symbol this;
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

Walter and I just discussed a potential solution for 2628 that would also take
care of other issues rather nicely. Aliasing a symbol to "this" would allow the
compiler to substitute this with this.symbol in contexts where lookup or type
conversions are attempted. This may obviate a need for opImplicitCast and would
also serve as implementation inheritance and others.


struct Tuple!(T...)
    T data;
    alias data this;

Using t[0] for a tuple would first figure out opIndex is not defined by the
struct itself and then would substitute t[0] with[0], which works.

struct X
    int x; 
    alias X x;

X a;
int b = a;
a = 42;

Neither use would compile, but the compiler substitutes:

int b = a.x;
a.x = 42;

so the code is working. If assignment is not desired:

struct S
    int _x;
    int x() { return x; }
    alias x this;

I'm posting this to open the floor for discussion.


Reply via email to