Thread overview | |||||
---|---|---|---|---|---|
|
September 28, 2013 possible nested class/struct alias this bug | ||||
---|---|---|---|---|
| ||||
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 } } } |
October 01, 2013 Re: possible nested class/struct alias this bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eberhard | 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
|
October 02, 2013 Re: possible nested class/struct alias this bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | 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.
|
Copyright © 1999-2021 by the D Language Foundation