November 23, 2017
On 2017-11-23 01:35, Jonathan M Davis wrote:

> It would make sense with something like the nodes of a linked list if they
> needed access to the container for some reason. Pretty much any case where a
> an instance of a nested class is going to be associated with a specific
> instance of its parent class and needs access to it would be a canditate.
> It's not that uncommon to see cases in C++ or Java where you'd pass a
> pointer to the "parent" to an instance of a nested class when it's created,
> and having outer built-in is kind of like that.
> 
> Personally, I've never had a use for it. I don't even use classes much in D,
> since I rarely need inheritance. And as I understand it, most D programs
> don't use classes very heavily for that very reason. So, I have no idea how
> common it is to use nested classes in this manner, but I expect that someone
> has found it useful at some point.
> 
> I thought that this meaning of static for nested classes came from Java, but
> it's been a while since I've done much with Java, so I don't know.

Yeah, it's very similar in D and Java.

Another example that comes from Java (before version 8) is to have a class with a nested anonymous class that implements an interface. It's very common in Java for event handling or similar actions. Other languages like D or Java 8 would use a delegate/lambda for the same thing. Something like:

class WindowController
{
    Button button;
    Window window;

    this()
    {
        button = new Button;
        window = new Window;
        button.onClick = new class() Clickable {
            window.close();
        };
    }
}

The anonymous class could have been a named class as well and to be able to access the "window" instance variable of the controller it needs to have access to the outer context and cannot be static.

-- 
/Jacob Carlborg
November 24, 2017
On 11/22/17 7:35 PM, Jonathan M Davis wrote:
> On Thursday, November 23, 2017 00:17:46 A Guy With a Question via
> Digitalmars-d-learn wrote:
>>> here as non-static, nested class is associated with a specific
>>> instance of the class and has access to that class instance via
>>> its outer member.
>>>
>>> - Jonathan M Davis
>>
>> Hmmm...now you have me very intrigued. What is a use-case where
>> you'd want to use a non-static embedded class? Sorry if I'm
>> asking too many questions. But there's a lot to dig into with
>> this language.
> 
> It would make sense with something like the nodes of a linked list if they
> needed access to the container for some reason. Pretty much any case where a
> an instance of a nested class is going to be associated with a specific
> instance of its parent class and needs access to it would be a canditate.
> It's not that uncommon to see cases in C++ or Java where you'd pass a
> pointer to the "parent" to an instance of a nested class when it's created,
> and having outer built-in is kind of like that.

The intent is exactly for porting Java code that uses either anonymous classes or inner classes.

-Steve
1 2
Next ›   Last »