digitalmars.D.learn - Access outer member of struct from inner struct
- Andrey (4/38) Apr 02 2019 Hello,
- Q. Schroll (6/45) Apr 02 2019 You cannot access stuff in Outer because Inner objects are not
- Jacob Carlborg (6/13) Apr 02 2019 It works if the struct is nested inside a function [1]. I thought it
- Q. Schroll (12/24) Apr 02 2019 The reason it works inside a function is that the struct has a
Hello, In this example how can I access the members "read" and "q" of struct Outer from Inner struct?struct Outer { ulong q = 1; Inner inner; void read(ulong value) { q += value; } void run() { q.writeln; read(5); } struct Inner { void write(string text) { read(text.length); writeln(q); } } } void main() { Outer ttt; ttt.run(); }During compilation I get:onlineapp.d(55): Error: this for read needs to be type Outer not type Inner onlineapp.d(56): Error: need this for q of type ulong
Apr 02 2019
On Tuesday, 2 April 2019 at 18:20:09 UTC, Andrey wrote:Hello, In this example how can I access the members "read" and "q" of struct Outer from Inner struct?After removing the calls to writeln, the error I get is:struct Outer { ulong q = 1; Inner inner; void read(ulong value) { q += value; } void run() { q.writeln; read(5); } struct Inner { void write(string text) { read(text.length); writeln(q); } } } void main() { Outer ttt; ttt.run(); }During compilation I get:onlineapp.d(55): Error: this for read needs to be type Outer not type Inner onlineapp.d(56): Error: need this for q of type ulong`this` for `read` needs to be type `Outer` not type `Inner`You cannot access stuff in Outer because Inner objects are not outer objects and don't implicitly own an Outer object. In your Inner method `write`, there is no Outer object present at all to call the method on.
Apr 02 2019
On 2019-04-02 20:44, Q. Schroll wrote:After removing the calls to writeln, the error I get is:It works if the struct is nested inside a function [1]. I thought it would work nested inside a struct too. [1] https://dlang.org/spec/struct.html#nested -- /Jacob Carlborg`this` for `read` needs to be type `Outer` not type `Inner`You cannot access stuff in Outer because Inner objects are not outer objects and don't implicitly own an Outer object. In your Inner method `write`, there is no Outer object present at all to call the method on.
Apr 02 2019
On Tuesday, 2 April 2019 at 18:52:07 UTC, Jacob Carlborg wrote:On 2019-04-02 20:44, Q. Schroll wrote:The reason it works inside a function is that the struct has a hidden pointer to the function context. The function's local values actually exist when an object of that struct type is being instantiated. The main difference between a struct nested in a function and one inside another struct is that the one in a function cannot¹ be created outside of that function while constructing the latter is possible the way you think it is: Outer.Inner innerObj = Outer.Inner(parameters); ¹ You can using reflection and stuff like that, but it's still broken if it uses the context.After removing the calls to writeln, the error I get is:It works if the struct is nested inside a function [1]. I thought it would work nested inside a struct too. [1] https://dlang.org/spec/struct.html#nested`this` for `read` needs to be type `Outer` not type `Inner`You cannot access stuff in Outer because Inner objects are not outer objects and don't implicitly own an Outer object. In your Inner method `write`, there is no Outer object present at all to call the method on.
Apr 02 2019