nested class inheritance

2011-05-31 Thread Michael Shulman
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


Re: nested class inheritance

2011-05-31 Thread Simen Kjaeraas
On Tue, 31 May 2011 20:17:23 +0200, Michael Shulman  
 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


Re: nested class inheritance

2011-05-31 Thread Michael Shulman
On Tue, May 31, 2011 at 2:24 PM, Simen Kjaeraas  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


Re: nested class inheritance

2011-06-01 Thread Simen Kjaeraas
On Wed, 01 Jun 2011 01:57:52 +0200, Michael Shulman  
 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