www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - possible nested class/struct alias this bug

reply "Eberhard" <eberhardhoepfner gmx.de> writes:
Hello,

I came across this unexpected error in the first example, so I 
tested similar scenarios, but couldn't make any sense of it.

Thanks, Eberhard.


class A {
    void foo() {}

    static class B {
       private A a;
       alias a this;
		
       void bar() {
          this.foo(); // works fine
          foo();      // Error: this for `foo` needs to be type 
`A` not type `A.B`
       }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class A {
    void foo() {}
}

class B {
    void foo() {}
	
    static class C {
       private A a;
       alias a this;
		
       void bar() {
          this.foo(); // works fine
          foo();      // Error: this for `foo` needs to be type 
`B` not type `B.C`
       }
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class A {
    void foo() {}
}

class B {
    // no foo
	
    static class C {
       private A a;
       alias a this;
		
       void bar() {
          this.foo(); // works fine
          foo();      // works fine
       }
    }
}
Sep 28 2013
parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 09/28/13 17:34, Eberhard wrote:
 I came across this unexpected error in the first example, so I tested similar
scenarios, but couldn't make any sense of it.
The symbols are looked up in parent scope first, the implicit conversion happens later. I'm not sure what the right order /should/ be; unfortunately making this situation an error would have compiler perf implications (couldn't then stop the lookup after the first match). artur
Oct 01 2013
parent "Eberhard" <eberhardhoepfner gmx.de> writes:
On Tuesday, 1 October 2013 at 17:35:07 UTC, Artur Skawina wrote:
 The symbols are looked up in parent scope first, the implicit
 conversion happens later.
I expected the compiler to check the aliasThis member right after the current scope. The language reference only says, that undefined lookups are forwarded to the aliasThis member. I think it should be exactly like with template mixins: import std.stdio, std.cstream; mixin template Template() { void foo() { writeln("Template.foo"); } } class Class { void foo() { writeln("Class.foo"); } } class Outer { void foo() { writeln("Outer.foo"); } class Inner { static const bool mixinTemplate = false; static if (mixinTemplate) { mixin Template bar; } else { private Class bar = new Class; alias bar this; } //void foo() { // writeln("Inner.foo"); //} void test() { foo(); this.foo(); this.outer.foo(); bar.foo(); } } void test() { Inner inner = new Inner; inner.test(); } } void main(string[] args) { Outer outer = new Outer; outer.test(); din.getc(); } mixinTemplate == true: Template.foo Template.foo Outer.foo Template.foo mixinTemplate == false: Outer.foo <-- should be Class.foo Class.foo Outer.foo Class.foo If the the inner class were static, mixinTemplate == false would not compile. aliasThis is just a dynamic mixin, right? Eberhard.
Oct 02 2013