July 16, 2006
On 2006-07-16 08:19:04 -0700, Stewart Gordon <smjg_1998@yahoo.com> said:

> Jarrett Billingsley wrote:
>> "Derek Parnell" <derek@psych.ward> wrote in message news:op.tcpklel16b8z09@ginger.vic.bigpond.net.au...
>> 
>>> Excuse my ignorance, but what does an inner class provide that other alternatives can't?
>> 
>> Well, what does a nested function provide that a separate, external function doesn't?
>> 
>> - Encapsulation
>> - Access to outer variables
> 
> Not just _access_ to outer variables, but the concept of outer variables in the first place.
> 
>> - Is a Cool Thing (TM) ;)
> 
> An inner class is basically syntactic sugar for a static nested class that includes a reference to an object of the enclosing class as one of its members.  For example,
> 
>      class Board {
>          class Cell {
>              this() {
>                  ...
>              }
>          }
>      }
> 
> is equivalent to
> 
>      class Board {
>          static class Cell {
>              private Board _outer;
>              this(Board b) {
>                  _outer = b;
>                  ...
>              }
>          }
>      }
> 
> However, when using the static nested class approach, referring to the whole of the 'enclosing' object, and not just to a specific member thereof, becomes trivial.  OTOH, to access the outer object from an inner class, you have to create a property in the outer class
> 
>      class Board {
>          Board theBoard() { return this; }
>      }
> 
> and then use it within the inner class.  This is a pinch of salt in the syntactic sugar that is inner classes.  An 'outer' keyword would indeed be nicer.
> 
> You could ask what access level outer should have.  For example, should
> it be acceptable to do, from outside, something like this?
> 
>      Cell c;
>      ...
>      Board b = c.outer;
> 
> Stewart.

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.

July 16, 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")
July 17, 2006
In article <e9e8h8$1asn$1@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 had outer.new Cell and all my braces closed.  Did you try to compile yours?




July 17, 2006
S. wrote:
> In article <e9e8h8$1asn$1@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 :(.
July 17, 2006
In article <e9gck1$grg$1@digitaldaemon.com>, Frits van Bommel says...
>
>S. wrote:
>> In article <e9e8h8$1asn$1@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 :(.

The problem I discovered was in the error message and dlint.  Dlint was highlighting that line and the compiler was generating an error about an indentifier without quoting it.  It just so happened my variable name fit in strangely with the message -- leaving me highly confused.

Everything works now.

-S.


1 2
Next ›   Last »