Hi,
I have a class which defines a nested class:
class Outer1 {
class Inner1 { }
}
Now I want to inherit from Outer1, to modify its behavior in a way
which also involves modifying the behavior of the corresponding inner
objects. My first instinct was to write
class Outer2 : Outer1 {
class Inner2 : Inner1 { }
}
but the compiler does not allow this. I guess that a nested class can
only be subclassed by a class nested in the same outer class.
Obviously it doesn't make sense to subclass a nested class in
arbitrary other places, but when nested in a subclass of the outer
class it seems sensible to me.
I have thought of a workaround with 'alias this':
class Outer2 : Outer1 {
class Inner2 {
Inner1 _self;
alias _self this;
this() {
_self = this.outer.new Inner1();
}
}
}
This seems to work, but requires manually calling all the constructors
of Inner1 from corresponding constructors of Inner2. Is there a better
way to do what I am after?
Thanks!
Mike

I have thought of a workaround with 'alias this':
class Outer2 : Outer1 {
class Inner2 {
Inner1 _self;
alias _self this;
this() {
_self = this.outer.new Inner1();
}
}
}
This seems to work, but requires manually calling all the constructors
of Inner1 from corresponding constructors of Inner2. Is there a better
way to do what I am after?

Does your inner class require implicit access to the outer class? That is=

could a static inner class work?

Thanks for the suggestion. Yes, my inner class does require access to
the outer class. I suppose it doesn't have to be *implicit* access;
the inner class could just keep an explicit reference to the outer
class. (Is there ever a situation in which the implicit-ness of the
'outer' reference is necessary, rather than just convenient?)
I've also realized that my proposed workaround actually doesn't work,
because 'alias this' doesn't actually behave like subclassing with
respect to references. That is, if Inner2 is 'alias this'ed to
Inner1, and I try to pass an Inner2 object to a function that's
expecting an Inner1, it actually just passes the _self Inner1 object
which knows nothing about Inner2 any more--right?
Mike

I've also realized that my proposed workaround actually doesn't work,
because 'alias this' doesn't actually behave like subclassing with
respect to references. That is, if Inner2 is 'alias this'ed to
Inner1, and I try to pass an Inner2 object to a function that's
expecting an Inner1, it actually just passes the _self Inner1 object
which knows nothing about Inner2 any more--right?