Thread overview
nested class inheritance
May 31, 2011
Michael Shulman
May 31, 2011
Simen Kjaeraas
Jun 01, 2011
Michael Shulman
Jun 01, 2011
Simen Kjaeraas
May 31, 2011
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
May 31, 2011
On Tue, 31 May 2011 20:17:23 +0200, Michael Shulman <viritrilbia@gmail.com> wrote:

> 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? Example:


class A {
    static class AA {
    }
}

class B : A {
    static class BB : A.AA {
    }
}


-- 
  Simen
June 01, 2011
On Tue, May 31, 2011 at 2:24 PM, Simen Kjaeraas <simen.kjaras@gmail.com> wrote:
>> 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
June 01, 2011
On Wed, 01 Jun 2011 01:57:52 +0200, Michael Shulman <viritrilbia@gmail.com> wrote:

> 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?

Correct.

-- 
  Simen