Accessing outer class attribute from inner struct

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 29 01:30:24 PDT 2017


On Tuesday, 29 August 2017 at 07:59:40 UTC, Andre Pany wrote:
> On Monday, 28 August 2017 at 23:12:40 UTC, Moritz Maxeiner 
> wrote:
>>
>> In both cases S doesn't inherently how about C, which means a 
>> solution using default initialization is not feasible, as 
>> S.init can't know about any particular instance of C.
>> I don't think there's any way for you to avoid using a class 
>> constructor.
>
> Thanks for the explanation. I now tried to use a class and use 
> a static opIndex. But it seems from a static method you also 
> cannot access the attributes of a outer class :)

A nested class' outer property (when nested inside another class) 
is a class reference, which means we not only require a class 
instance of the outer class to reference, but also a class 
instance of the nested class to store said class reference to the 
other class in.
A static class method (by definition) is invoked without a class 
instance.
The two are inherently incompatible.

> [...]
>
> This seems like an unnecessary limitation...

I can only recommend reading the language specification w.r.t, 
nested classes [1] if it seems that way to you, because it is not.

[1] https://dlang.org/spec/class.html#nested



More information about the Digitalmars-d-learn mailing list