digitalmars.D.learn - Inner classes question
- Ivan Senji (24/24) Sep 10 2006 I know this (or something like this) has been discussed before but I
- Steve Horne (5/6) Sep 10 2006 Maybe: new super.I1 ???
- Ivan Senji (2/8) Sep 10 2006 I2 doesn't have a base class, so that wouldn't work.
- Steve Horne (11/12) Sep 10 2006 Yeah, I'm confusing myself.
- Ivan Senji (17/28) Sep 10 2006 Sorry (a bug in my example, but it doesn't change anything)
- David Medlock (3/41) Sep 11 2006 Make I1 static? This compiles, though I am not sure if it does the same...
- Ivan Senji (3/6) Sep 11 2006 Instance of a static inner class doesn't have access to outer members of...
- Ary Manzana (22/55) Sep 11 2006 In Java it's done this way:
- Ivan Senji (25/49) Sep 11 2006 Yeah, maybe it is a little strange but I wouldn't mind if D did it that
- Jarrett Billingsley (5/26) Sep 11 2006 Then move outerO into O :) Thus, outerO is initialized as the "this" fo...
- Ivan Senji (6/39) Sep 11 2006 Thanks Jarrett! I can't believe I didn't think of that, that will work
- Jarrett Billingsley (3/5) Sep 11 2006 That's what desks are for!
I know this (or something like this) has been discussed before but I have to ask anyway, let's say I have class O { class I1 { } class I2 { I1 a = new I1; //line 8 } } This is currently impossible because of: (8): outer class O 'this' needed to 'new' nested class I1 Once again the outer keyword is missing. The compiler is being very helpful by saying that I need outer's class this pointer, but it isn't telling me how to get it. (Is there any way?) For I2 to be created outer's class this must have existed and it must be stored in I2's instance so I don't see the need in any special syntax. new I1 should be enough. I know about the workaround solutions of passing O.this to I1 and I2 constructors but aren't inner classes here to avoid that very coding pattern? Something (adding outer?) must be done about this.
Sep 10 2006
On Sun, 10 Sep 2006 12:34:23 +0200, Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> wrote:(8): outer class O 'this' needed to 'new' nested class I1Maybe: new super.I1 ??? -- Remove 'wants' and 'nospam' from e-mail.
Sep 10 2006
Steve Horne wrote:On Sun, 10 Sep 2006 12:34:23 +0200, Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> wrote:I2 doesn't have a base class, so that wouldn't work.(8): outer class O 'this' needed to 'new' nested class I1Maybe: new super.I1 ???
Sep 10 2006
On Sun, 10 Sep 2006 13:11:47 +0200, Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> wrote:I2 doesn't have a base class, so that wouldn't work.Yeah, I'm confusing myself. The problem, I suspect, is putting 'new' in an initialiser. If it was in a (dynamic) method, the standard route to the outer-class instance would apply so it should just work, unless I'm missing something. Then again, putting 'new' in an initialiser just seems plain wrong to me anyway - not sure if that's a carry-over from other languages, or a real concern in D, though. -- Remove 'wants' and 'nospam' from e-mail.
Sep 10 2006
Steve Horne wrote:On Sun, 10 Sep 2006 13:11:47 +0200, Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> wrote:Sorry (a bug in my example, but it doesn't change anything) class O { class I1 { } class I2 { this() { I1 a = new I1; //line 10 -> same error } } }I2 doesn't have a base class, so that wouldn't work.Yeah, I'm confusing myself. The problem, I suspect, is putting 'new' in an initialiser. If it was in a (dynamic) method,the standard route to the outer-class instance would apply so it should just work, unless I'm missing something.That is what I would expect too but it doesn't happen (so we are both missing something)
Sep 10 2006
Ivan Senji wrote:Steve Horne wrote:Make I1 static? This compiles, though I am not sure if it does the same thing as Java.On Sun, 10 Sep 2006 13:11:47 +0200, Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> wrote:Sorry (a bug in my example, but it doesn't change anything) class O { class I1 { } class I2 { this() { I1 a = new I1; //line 10 -> same error } } }I2 doesn't have a base class, so that wouldn't work.Yeah, I'm confusing myself. The problem, I suspect, is putting 'new' in an initialiser. If it was in a (dynamic) method,the standard route to the outer-class instance would apply so it should just work, unless I'm missing something.That is what I would expect too but it doesn't happen (so we are both missing something)
Sep 11 2006
David Medlock wrote:Make I1 static? This compiles, though I am not sure if it does the same thing as Java.Instance of a static inner class doesn't have access to outer members of outer class instance.
Sep 11 2006
Ivan Senji wrote:Steve Horne wrote:In Java it's done this way: class O { class I1 { } class I2 { this() { I1 a = O.this.new I1; } } } "O.this" says "Give me the reference of O that contains me" and from that point you say "new I1", that belongs to O. The syntax is a bit (a lot?) strange, though... However, if there is no ambiguity in "new I1", then writing "O.this" is not necessary. So the example you sent works perfectly in Java, and I think it should work the same way in D. AryOn Sun, 10 Sep 2006 13:11:47 +0200, Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> wrote:Sorry (a bug in my example, but it doesn't change anything) class O { class I1 { } class I2 { this() { I1 a = new I1; //line 10 -> same error } } }I2 doesn't have a base class, so that wouldn't work.Yeah, I'm confusing myself. The problem, I suspect, is putting 'new' in an initialiser. If it was in a (dynamic) method,the standard route to the outer-class instance would apply so it should just work, unless I'm missing something.That is what I would expect too but it doesn't happen (so we are both missing something)
Sep 11 2006
Ary Manzana wrote:In Java it's done this way: class O { class I1 { } class I2 { this() { I1 a = O.this.new I1; } } } "O.this" says "Give me the reference of O that contains me" and from that point you say "new I1", that belongs to O. The syntax is a bit (a lot?) strange, though...Yeah, maybe it is a little strange but I wouldn't mind if D did it that way, just that it works.However, if there is no ambiguity in "new I1", then writing "O.this" is not necessary. So the example you sent works perfectly in Java, and I think it should work the same way in D.It would be nice. The current workaround I'm using can hardly be called a workaround at all: O outerO; class O { this() { outerO = this; } class I1 { } class I2 { this() { I1 a = outerO.new I1; } } } The only problem with this is that I am limited to only one instance of O this way :)
Sep 11 2006
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:ee46qa$2frb$1 digitaldaemon.com...The current workaround I'm using can hardly be called a workaround at all: O outerO; class O { this() { outerO = this; } class I1 { } class I2 { this() { I1 a = outerO.new I1; } } } The only problem with this is that I am limited to only one instance of O this way :)Then move outerO into O :) Thus, outerO is initialized as the "this" for each instance, and the inner classes can access outerO without the need for any kind of qualification.
Sep 11 2006
Jarrett Billingsley wrote:"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:ee46qa$2frb$1 digitaldaemon.com...Thanks Jarrett! I can't believe I didn't think of that, that will work perfectly :) If I had a wall nearby I would be banging my head into it for not thinking of that. Thanks again!! :)The current workaround I'm using can hardly be called a workaround at all: O outerO; class O { this() { outerO = this; } class I1 { } class I2 { this() { I1 a = outerO.new I1; } } } The only problem with this is that I am limited to only one instance of O this way :)Then move outerO into O :) Thus, outerO is initialized as the "this" for each instance, and the inner classes can access outerO without the need for any kind of qualification.
Sep 11 2006
"Ivan Senji" <ivan.senji_REMOVE_ _THIS__gmail.com> wrote in message news:ee4dj2$2onc$1 digitaldaemon.com...If I had a wall nearby I would be banging my head into it for not thinking of that.That's what desks are for!
Sep 11 2006