August 21 [Issue 24712] New: Derived outer class reference doesn't work right. | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=24712 Issue ID: 24712 Summary: Derived outer class reference doesn't work right. Product: D Version: D2 Hardware: All URL: http://dlang.org/ OS: All Status: NEW Severity: normal Priority: P3 Component: dmd Assignee: nobody@puremagic.com Reporter: turkeyman@gmail.com import std.stdio; class Outer1 { int value1 = 100; class Inner1 { void print() { writeln("value1 from Outer1: ", value1); } } Inner1 make() { return new Inner1; } } class Outer2 : Outer1 { int value2 = 200; class Inner2 : Outer1.Inner1 { override void print() { writeln("value1 from Outer1: ", value1); // <- no problem! writeln("value2 from Outer2: ", value2); // error : accessing non-static variable `value2` requires an instance of `Outer2` } } override Inner2 make() { return new Inner2; } } So, I define a base class which has an inner class, and then users derive from the base class, but may also derive an inner, but in that arrangement, outer references stop working! The idea is that outer class is a kind of plugin module, where people can define the global working state for their module, and the inner class is an instance that the application creates many of. Each instance should have access to its respective global working state, and the outer pointer seemed perfect for this... but it seems that from Inner2, it is only possible to access Outer1's scope! It looks like the up-pointer is typed incorrectly; it is typed as the base type, and not as the derived type which was assigned on creation. The new statement that creates Inner2 is in an override make() function, so it's definitely called from within Outer2's scope, and so can be sure the up-pointer given is for its parent Outer2... so why isn't the derived inner's up-pointer typed as the derived outer type? As an experiment, I tried to new an Inner2 from Outer1's scope, so that it would attempt to be created given a base context rather than the appropriate derived context, but it rejected the call appropriately: class Outer1 { .... Outer2.Inner2 makederived() { return new Outer2.Inner2; } } error : cannot construct nested class `Inner2` because no implicit `this` reference to outer class `Outer2` is available So, as I see it, there's no obvious reason for the up-pointer in the derived inner to not be of the derived outer's type... So, is there a bug? Or is there some design issue here that can be exploited to cause a failure somehow? -- |
Copyright © 1999-2021 by the D Language Foundation