Outer class reference oddity?
Manu
turkeyman at gmail.com
Tue Aug 20 09:48:27 UTC 2024
Some further odd observations:
If at the point of error, I refer `outer.value2`, I get: "error : undefined
identifier `outer`" ... which seems odd, because I thought `outer` was the
name of the implicit outer context?
So I tried `this.outer.value2`, and that worked! It's typed correctly too.
So... I guess there are 2 variables called `outer`, super.outer, and
this.outer? And for some reason I don't understand, *neither* of them are
present in the local namespace... but I can refer to them explicitly via
super.outer, or this.outer.
And for some reason that I don't understand, the local outer (this.outer)
does NOT shadow the parent outer (super.outer), which seems backwards.
Local definitions should always shadow base class members?
I'm not happy with the idea that there are 2 members called outer, because
they're both the same pointer! Hopefully it's dirty implementation magic
and they are actually the same member internally...
On Tue, 20 Aug 2024 at 18:48, Manu <turkeyman at gmail.com> wrote:
> Another thing I've never tried to use in D before; an outer class
> reference!
> As usual, I try to use a thing and it doesn't work...
>
> Here's a situation:
>
> 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?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20240820/5324e31d/attachment-0001.htm>
More information about the Digitalmars-d
mailing list