Lack of `outer` keyword makes inner class dup implossible

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Jul 17 09:07:28 PDT 2006


S. wrote:
> In article <e9e8h8$1asn$1 at digitaldaemon.com>, Frits van Bommel says...
>> S. Chancellor wrote:
>>> Exactly.  Interestingly though I tried this:
>>>
>>> class Board {
>>>     Board outer;
>>>     this() { outer = this; }
>>>     class Cell {
>>>         Cell dup() {
>>>             return new outer.Cell;
>>>         }
>>>     }
>>>
>>> and I get syntax errors.   This new expression stuff seems broken.
>> First thing you'll want to do is close all your braces. You missed one :).
>>
>> The 'new' syntax is a bit weird for inner classes I think. This should work:
>>
>> class Board {
>>     Board outer;
>>     this() { outer = this; }
>>     class Cell {
>>         Cell dup() {
>>             return outer.new Cell;
>>         }
>>     }
>> }
>>
>> I agree that 'new outer.Cell' is a more intuitive syntax for this, but 
>> that's just not the way it works...
>> This is also the syntax for 'new'ing an inner class in Java, IIRC. I 
>> remember tripping over the syntax a couple of times when I was using 
>> Java. (I haven't needed inner classes in D yet[1])
>>
>> See also http://www.digitalmars.com/d/class.html (almost at the very 
>> end, just above the heading "Anonymous Nested Classes")
> 
> I made some typo's -- that is not the verbatim code I was tring to compile.  I

Copy-paste is very handy when complaining about compile errors ;).
You might want to copy-paste the errors themselves too.

> had outer.new Cell and all my braces closed.  Did you try to compile yours?

Yes I did. And I just did it again. Compiles just fine:

     D:\Temp>cat test.d
     class Board {
          Board outer;
          this() { outer = this; }
          class Cell {
              Cell dup() {
                  return outer.new Cell;
              }
          }
     }
     D:\Temp>dmd -c test.d

     D:\Temp>

That's DMD v0.161 though. v0.162 has an unrelated bug causing it to 
segfault when compiling some of my code :(.



More information about the Digitalmars-d mailing list