generalizing hiding rules

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Oct 2 10:33:07 PDT 2009


I was just playing with some nested classes:

class Base {
     int x;
}

class A {
     private int x, y;
     class B : Base {
         int z;
         this() {
             x = 42;
             y = 43;
             this.outer.x = 44;
             z = 45;
         }
     }
}

In the code above, if you just write "x" in A.B's constructor, Base.x is 
fetched. So Base.x hides this.outer.x.

Disallowing hiding vertically in nested scopes has been quite 
successful. Also, D's no-hijack stance is also shaping up to be a hit. I 
am therefore thinking - why not apply the no-hijack rule throughout the 
language?

If one symbol leads to working code through two different lookups, an 
ambiguity error would be issued. The only exception would be if one 
symbol is scoped and the other is at module scope (for the reasons I 
discussed in another post).

I wonder to what extent this would break modular code, or foster more 
intelligible code. With this rule in tow, A.B.this() would have to use 
Base.x and this.outer.x to access the two x's.

What do you think?


Andrei



More information about the Digitalmars-d mailing list