nested class inheritance

Steven Schveighoffer schveiguy at yahoo.com
Fri Jul 13 13:58:15 PDT 2012


On Fri, 13 Jul 2012 13:59:54 -0400, Gor Gyolchanyan  
<gor.f.gyolchanyan at gmail.com> wrote:

> Doesn't this make sense?
>
> class Fruit
> {
>     class Seed { }
> }
>
> class Apple: Fruit
> {
>      class AppleSeed: Fruit.Seed { }
> }
>
> This means, that as Fruit.Seed needs access to enclosing Fruit,  
> AppleSeed,
> being a Fruit.Seed will also have access to enclosing Fruit, which is  
> Apple.
> DMD 0.259 has this to say:
>
> Error: class main.Apple.AppleSeed is nested within Apple, but super class
> Seed is nested within Fruit
>
> Which doesn't make sense, IMO. What do you guys think about this? Who  
> makes
> more sense me or DMD 2.059?
>


This is what you are asking for:

class A {}

class B : A {}

class C
{
    A member;
}

class D : C
{
    override B member;
}

You can mimic it via:

class C
{
    protected A _member;
    @property inout(A) member() inout { return _member; }
}

class C : B
{
    override @property inout(B) member() inout { return  
cast(inout(B))_member; }
}

I have had many times where I had to do this workaround (with casting), or  
store extra references, but it would be nice to have this simply work  
depending on the type.  I don't think anyone considered doing this with  
outer, it certainly would be a valid feature.  I'd like to have the  
general feature as well.  Covariant fields...

-Steve


More information about the Digitalmars-d mailing list