nested class inheritance

Guillaume Chatelet chatelet.guillaume at gmail.com
Sat Jul 14 02:36:23 PDT 2012


On 07/13/12 21:41, Andrei Alexandrescu wrote:
> On 7/13/12 3:36 PM, Gor Gyolchanyan wrote:
>> The initial question was: why does DMD 2.059 reject this if this makes
>> sense?
>> It's not even a new feature. It's a (possibly) new (and apparently
>> sensible) use case of an existing feature.
> 
> I think the simple answer is there was no provision for it. The feature
> copies the Java feature, and I don't think Java has something like what
> you defined.
> 
> Andrei
> 

class Fruit {
    class Seed {
    }
}

class Apple extends Fruit {
    class AppleSeed extends Fruit.Seed {
        Apple getOuter() {
            return Apple.this;
        }
    }
}

class Main {
    public static void main(String[] args) {
        final Apple apple = new Apple();
        final Apple.AppleSeed appleSeed = apple.new AppleSeed();
        assert (appleSeed instanceof Fruit.Seed);
        assert (apple == appleSeed.getOuter());
        assert (appleSeed.getOuter() instanceof Apple);
        assert (appleSeed.getOuter() instanceof Fruit);
    }
}

This is valid Java code actually and I agree with Gor I would have
expected it to work with D.

Guillaume


More information about the Digitalmars-d mailing list