www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inner classes question

reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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
parent reply Steve Horne <stephenwantshornenospam100 aol.com> writes:
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 I1

Maybe: new super.I1 ??? -- Remove 'wants' and 'nospam' from e-mail.
Sep 10 2006
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Steve Horne wrote:
 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 I1

Maybe: new super.I1 ???

I2 doesn't have a base class, so that wouldn't work.
Sep 10 2006
parent reply Steve Horne <stephenwantshornenospam100 aol.com> writes:
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
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Steve Horne wrote:
 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,

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 } } }
 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
next sibling parent reply David Medlock <noone nowhere.com> writes:
Ivan Senji wrote:
 Steve Horne wrote:
 
 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,

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 } } }
 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)

Make I1 static? This compiles, though I am not sure if it does the same thing as Java.
Sep 11 2006
parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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
prev sibling parent reply Ary Manzana <asterite gmail.com> writes:
Ivan Senji wrote:
 Steve Horne wrote:
 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,

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 } } }
 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)

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. Ary
Sep 11 2006
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Jarrett Billingsley wrote:
 "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.

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!! :)
Sep 11 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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