`this` and nested structs

Radu rad.racariu at gmail.com
Thu May 10 04:16:57 UTC 2018


On Thursday, 10 May 2018 at 03:23:50 UTC, Mike Franklin wrote:
> Consider the following code:
>
> ---
> struct S
> {
>     // intentionally not `static`
>     struct SS
>     {
>         int y() { return x; }  // Error: need `this` for `x` of 
> type `int`
>     }
>
>     int x;
>     SS ss;
> }
>
> void main()
> {
>     S s;
>     s.ss.y();
> }
> ---
>
> If I change `return x;` to `return this.x;` then of course it 
> emits the following error:
>
> Error: no property `x` for type `SS`
>
> My understanding is that `SS` should have a context pointer to 
> an instance of `S`, but how do I navigate the members of `S` 
> and `SS`.  Is this a bug?
>
> Thanks,
> Mike
>
> My understanding is that nested structs have an implicit 
> context pointer to their containing scope.

Nesting with hidden context pointer is only for nested structs 
inside functions.
https://dlang.org/spec/struct.html#nested

This is a source a confusion unfortunately.


More information about the Digitalmars-d-learn mailing list