`this` and nested structs

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu May 10 06:48:15 UTC 2018

On Thursday, May 10, 2018 06:31:09 Mike Franklin via Digitalmars-d-learn 
> On Thursday, 10 May 2018 at 06:22:37 UTC, Jonathan M Davis wrote:
> > Structs don't have that.
> Should they?

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 Davis

More information about the Digitalmars-d-learn mailing list