digitalmars.D.learn - Nested Structs
- d coder (9/9) Dec 27 2010 Greetings All
- Steven Schveighoffer (18/25) Dec 27 2010 A struct nested in a class does not have a hidden "outer" pointer as a
- bearophile (5/8) Dec 27 2010 But I think this will eventually change, once this part is implemented:
- Steven Schveighoffer (14/24) Dec 27 2010 That is implemented, but notice the conditions that form a nested struct...
- d coder (27/33) Dec 27 2010 Thanks Steve
Greetings All I have a situation where I have a struct nested inside a class. I would like to make the enclosing class' members visible inside the nested struct's constructor. I see that such preposition is feasible for nested classes, but not for nested structs. Why so? Are there some alternative constructs which could give me this behavior for nested constructs? Regards - Cherry
Dec 27 2010
On Mon, 27 Dec 2010 08:54:37 -0500, d coder <dlang.coder gmail.com> wrote:Greetings All I have a situation where I have a struct nested inside a class. I would like to make the enclosing class' members visible inside the nested struct's constructor. I see that such preposition is feasible for nested classes, but not for nested structs. Why so?A struct nested in a class does not have a hidden "outer" pointer as a nested class does. It's because a struct is generally more bare-bones than a class (which has loads of hidden pieces: vtable, interfaces, classinfo, etc.). Also, instantiating such a struct does not tie it to a class instance.Are there some alternative constructs which could give me this behavior for nested constructs?You need to implement this behavior on your own: class C { struct S { private C _outer; this(C c) { this._outer = c; _outer.x = 5;} // access enclosing class instance via _outer. } int x; } -Steve
Dec 27 2010
Steven Schveighoffer:A struct nested in a class does not have a hidden "outer" pointer as a nested class does.But I think this will eventually change, once this part is implemented: http://www.digitalmars.com/d/2.0/struct.htmlNested Structs: A nested struct is a struct that is declared inside the scope of a function or a templated struct that has aliases to local functions as a template argument. Nested structs have member functions. It has access to the context of its enclosing scope (via an added hidden field).<Bye, bearophile
Dec 27 2010
On Mon, 27 Dec 2010 12:50:47 -0500, bearophile <bearophileHUGS lycos.com> wrote:Steven Schveighoffer:That is implemented, but notice the conditions that form a nested struct do not include "inside a class" or "inside a struct" (that "templated struct that has aliases to local functions as a template argument" condition is pretty specific, I'm talking about just putting it in any old struct). This is decidedly different from a nested class: http://www.digitalmars.com/d/2.0/class.html#nested "A nested class is a class that is declared inside the scope of a function or another class. A nested class has access to the variables and other symbols of the classes and functions it is nested inside" This is what the OP was desiring. -SteveA struct nested in a class does not have a hidden "outer" pointer as a nested class does.But I think this will eventually change, once this part is implemented: http://www.digitalmars.com/d/2.0/struct.htmlNested Structs: A nested struct is a struct that is declared inside the scope of a function or a templated struct that has aliases to local functions as a template argument. Nested structs have member functions. It has access to the context of its enclosing scope (via an added hidden field).<
Dec 27 2010
A struct nested in a class does not have a hidden "outer" pointer as a nested class does. =A0It's because a struct is generally more bare-bones =thana class (which has loads of hidden pieces: vtable, interfaces, classinfo, etc.). =A0Also, instantiating such a struct does not tie it to a class instance.Thanks Steve As far as I am concerned, this is a very limiting behavior wrt structs nested inside classes. I have also observed that if you define a constant (using enum for example) inside the enclosing class, the constant remains visible in the nested struct too. So I believe the language is making the hidden "outer" scope visible to the nested struct (or maybe there is some other mechanism for enums that I am not aware of).You need to implement this behavior on your own:Actually I am trying to define a DSEL and the end users are not typically programmers. I am in a situation where the end-user decides the unit in an enclosing class (unit of length as an inch for example) and later whenever he instantiates a line or a shape, the particular unit is automatically considered at the time of instantiation. For my application, I need value semantics and therefor using nested classes in place of structs can not be considered. Making the end-users pass the unit or its context (in form of this pointer) everytime they instantiate a shape would be atrocious. It would defeat the very purpose of letting the end-user define a unit. I know I am not on the right mailing group, but I want to ask for this particular language enhancement. I believe this would make the behavior of the language more straight wrt nested structs inside classes (are not these beasts expected to serve like nested classes or even structs nested inside function scopes?). I hope my requirement is generic enough and fits the bill for an enhancement request. Regards - Cherry
Dec 27 2010