digitalmars.D.learn - `this` and nested structs
- Mike Franklin (29/29) May 09 2018 Consider the following code:
- Radu (5/34) May 09 2018 Nesting with hidden context pointer is only for nested structs
- Jonathan M Davis (7/9) May 09 2018 A non-static struct inside a function does, but I suspect that you're
- Mike Franklin (2/3) May 09 2018 Should they?
- Jonathan M Davis (18/21) May 09 2018 Honestly, I don't think that classes should have it, but changing it now
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.
May 09 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.
May 09 2018
On Thursday, May 10, 2018 03:23:50 Mike Franklin via Digitalmars-d-learn wrote:My understanding is that nested structs have an implicit context pointer to their containing scope.A non-static struct inside a function does, but I suspect that you're thinking about non-static nested classes (within classes), which I believe was done to be compatible with Java (in order to make porting code easier). Structs don't have that. - Jonathan M Davis
May 09 2018
On Thursday, 10 May 2018 at 06:22:37 UTC, Jonathan M Davis wrote:Structs don't have that.Should they?
May 09 2018
On Thursday, May 10, 2018 06:31:09 Mike Franklin via Digitalmars-d-learn wrote:On Thursday, 10 May 2018 at 06:22:37 UTC, Jonathan M Davis wrote:Honestly, I don't think that classes should have it, but changing it now would break code (most notably, DWT). IMHO, it would have been far better to make it explicit like it is in C++, especially since there's no technical reason why it needs to be built in. But regardless, when you consider how structs work, having a nested struct inside a struct have a pointer to its outer struct would be a serious problem, because the pointer would become invalid as soon as the struct was moved, which happens quite frequently with structs in D when they're passed around. Having a nested struct within a class have a reference to its parent would be more debatable, but it would probably run afoul of how init works, since the init value for the struct would have to be known at compile-time, whereas the reference for the class couldn't be. And anyone who wants any kind of reference to the outer class to be in the struct can just pass it to the struct's constructor. Adding such a feature to structs would just be unnecessary magic. - Jonathan M DavisStructs don't have that.Should they?
May 09 2018