July 13, 2012 nested class inheritance | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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?
--
Bye,
Gor Gyolchanyan.
|
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On 7/13/12 1:59 PM, Gor Gyolchanyan 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?
Untested:
class Fruit
{
static class Seed { }
}
class Apple: Fruit
{
static class AppleSeed: Fruit.Seed { }
}
Andrei
|
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On Friday, 13 July 2012 at 18:00:05 UTC, Gor Gyolchanyan 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?
I'd say DMD makes more sense. Seed has knowledge of itself AND Fruit. So if it's following the same rules and for how it makes sense, then unless you add static to Seed, then you won't get it to work. Reminds me of when I used Java years ago experimenting with inner classes, came to a distinct conclusion of object referencing and why it didn't want to work without an outer object.
Consider: fails on seed, not seed2.
class Fruit {
int x;
class Seed {
void oneMoreToX() {
x++; //knows about Fruit.x, even if not instantiated
}
}
static class Seed2 {
void oneMoreToX() {
// x++; //fails to compile, no knowledge of Fruit
}
}
}
class Apple: Fruit {
class AppleSeed: Fruit.Seed { } //fails (no outer object (Fruit.x) and makes no sense)
class AppleSeed2: Fruit.Seed2 { } //works fine
}
|
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu Attachments:
| On Fri, Jul 13, 2012 at 10:19 PM, Andrei Alexandrescu < SeeWebsiteForEmail@erdani.org> wrote: > On 7/13/12 1:59 PM, Gor Gyolchanyan 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? >> > > Untested: > > class Fruit > { > static class Seed { } > } > > class Apple: Fruit > { > static class AppleSeed: Fruit.Seed { } > } > > > Andrei > The whole point is to have it not static. I need it to be properly nested with the this.outer. -- Bye, Gor Gyolchanyan. |
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow Attachments:
| On Fri, Jul 13, 2012 at 10:25 PM, Era Scarecrow <rtcvb32@yahoo.com> wrote: > On Friday, 13 July 2012 at 18:00:05 UTC, Gor Gyolchanyan 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? >> > > I'd say DMD makes more sense. Seed has knowledge of itself AND Fruit. So > if it's following the same rules and for how it makes sense, then unless > you add static to Seed, then you won't get it to work. Reminds me of when I > used Java years ago experimenting with inner classes, came to a distinct > conclusion of object referencing and why it didn't want to work without an > outer object. > > Consider: fails on seed, not seed2. > > class Fruit { > int x; > class Seed { > void oneMoreToX() { > x++; //knows about Fruit.x, even if not instantiated > } > } > > static class Seed2 { > void oneMoreToX() { > // x++; //fails to compile, no knowledge of Fruit > } > } > } > > class Apple: Fruit { > class AppleSeed: Fruit.Seed { } //fails (no outer object (Fruit.x) and > makes no sense) > class AppleSeed2: Fruit.Seed2 { } //works fine > } > I need Seed to have a reference to Fruit because there will be more then one derivative of Seed in the same derivative of Fruit and they all need to store data inside the fruit and communicate with each other in that way. That's why Seed can't be static. What can I do to have multiple seeds in the same fruit with all seed-fruit-seed communication hidden in the base classes? -- Bye, Gor Gyolchanyan. |
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gor Gyolchanyan | On Friday, 13 July 2012 at 18:34:06 UTC, Gor Gyolchanyan wrote:
> I need Seed to have a reference to Fruit because there will be more then one derivative of Seed in the same derivative of Fruit and they all need to store data inside the fruit and communicate with each other in that way. That's why Seed can't be static. What can I do to have multiple seeds in the same fruit with all seed-fruit-seed communication hidden in the base classes?
Then perhaps have the inherited class within fruit?
class Fruit {
class Seed {}
class Appleseed : Seed {}
}
|
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | "Era Scarecrow" , dans le message (digitalmars.D:172269), a écrit :
> class Fruit {
> int x;
> class Seed {
> void oneMoreToX() {
> x++; //knows about Fruit.x, even if not instantiated
> }
> }
>
> static class Seed2 {
> void oneMoreToX() {
> // x++; //fails to compile, no knowledge of Fruit
> }
> }
> }
>
> class Apple: Fruit {
> class AppleSeed: Fruit.Seed { } //fails (no outer object
> (Fruit.x) and makes no sense)
> class AppleSeed2: Fruit.Seed2 { } //works fine
> }
AppleSeed does have a outer Fruit, and this Fruit happens to be an Apple. I don't see what is the issue, and what prevents the langage to support such AppleSeed. I'm not saying there is nothing that prevents the langage to support such pattern, I am not used to inner classes. Pleas enlighten me.
|
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | "Era Scarecrow" , dans le message (digitalmars.D:172272), a écrit :
> Then perhaps have the inherited class within fruit?
>
> class Fruit {
> class Seed {}
> class Appleseed : Seed {}
> }
But then AppleSeed doesn't know about Apple....
|
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christophe Travert Attachments:
| On Fri, Jul 13, 2012 at 10:55 PM, Christophe Travert < travert@phare.normalesup.org> wrote: > "Era Scarecrow" , dans le message (digitalmars.D:172272), a écrit : > > Then perhaps have the inherited class within fruit? > > > > class Fruit { > > class Seed {} > > class Appleseed : Seed {} > > } > > But then AppleSeed doesn't know about Apple.... > > Right. And Fruit.Seed can't know about Apple either, because there are tons of fruits like Apple, each of which can have tons of different Seeds. I really didn't get why my original example didn't make sense. -- Bye, Gor Gyolchanyan. |
July 13, 2012 Re: nested class inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christophe Travert | On Friday, 13 July 2012 at 18:55:28 UTC, travert@phare.normalesup.org (Christophe Travert) wrote:
> "Era Scarecrow" , dans le message (digitalmars.D:172272), a écrit :
>> Then perhaps have the inherited class within fruit?
>>
>> class Fruit {
>> class Seed {}
>> class Appleseed : Seed {}
>> }
>
> But then AppleSeed doesn't know about Apple....
So I forgot the apple :P But then inner inheritance won't work.
class Fruit {
class Seed {}
class Apple {
class Appleseed : Seed {} //same error as before
}
}
If you make seed static, then Appleseed & apple will have access to Fruit but seed (by itself) won't. Other combinations faile as far as I can tell. How far you can take inner class inheritance I don't know, but it seems not that many levels.
|
Copyright © 1999-2021 by the D Language Foundation