Lack of `outer` keyword makes inner class dup implossible

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun Jul 16 13:45:27 PDT 2006


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



More information about the Digitalmars-d mailing list