[Issue 2631] New: alias symbol this;

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 28 17:00:23 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2631

           Summary: alias symbol this;
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: andrei at metalanguage.com


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.

Example:

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 t.data[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.


-- 



More information about the Digitalmars-d-bugs mailing list