alias this and shadowing

Jonathan Shamir via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 15 08:01:27 PDT 2017


I think the following example is legitimate code that should work:

struct S1 {
     void foo() {
         writeln("S1.foo()");
     }
     void foo(ulong x) {
         writefln("S1.foo(%d)", x);
     }
}

struct S2 {
     S1 s;
     alias s this;

     void foo(string str) {
         writeln("S2.foo(%s)", str);
     }
}

void main() {
     S2 s;

     s.foo("hahaha");
     s.foo(100);  // XXX this doesn't compile, since there's no 
S2.foo that accepts an int
}


To quote Andrei, if it looks like it should work, it should. 
(Also something about turtles).

My original code did something a bit different, just to show 
another example of why this behavior looks fundamentally broken 
to me:

struct S2 {
     S1 s;
     alias s this;
     @disable void foo(ulong); // calling foo(ulong) doesn't make 
sense in the context of S2
}

Now, calling S2.foo() won't compile, since S2 only knows one 
prototype for foo which accepts a ulong.


More information about the Digitalmars-d mailing list