digitalmars.D.learn - toString contains null for struct with function/method
- number (25/25) Apr 08 2018 the write() shows a 'null' if the struct has a function/method.
- Paul Backus (6/31) Apr 08 2018 S2 is a nested struct [1], which means it has a hidden pointer
- number (4/11) Apr 09 2018 Aha, its the nesting. And if i access the outer scope in the
the write() shows a 'null' if the struct has a function/method. why is that? ``` import std.stdio; void main() { struct S { int i; } S s; writeln(s); // S(0) writeln(typeid(s).sizeof); // 8 struct S2 { int i; this(this){} } S2 s2; import std.conv: to; writeln(s2); // S2(0, null) writeln(typeid(s2).sizeof); // 8 } ```
Apr 08 2018
On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote:the write() shows a 'null' if the struct has a function/method. why is that? ``` import std.stdio; void main() { struct S { int i; } S s; writeln(s); // S(0) writeln(typeid(s).sizeof); // 8 struct S2 { int i; this(this){} } S2 s2; import std.conv: to; writeln(s2); // S2(0, null) writeln(typeid(s2).sizeof); // 8 } ```S2 is a nested struct [1], which means it has a hidden pointer field that's used to access its enclosing scope. If you change the definition to `static struct S2`, you'll get the same output for both structs. [1] https://dlang.org/spec/struct.html#nested
Apr 08 2018
On Sunday, 8 April 2018 at 15:51:05 UTC, Paul Backus wrote:On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote:Aha, its the nesting. And if i access the outer scope in the function its not null anymore. Thank you!writeln(s2); // S2(0, null)S2 is a nested struct [1], which means it has a hidden pointer field that's used to access its enclosing scope. If you change the definition to `static struct S2`, you'll get the same output for both structs. [1] https://dlang.org/spec/struct.html#nested
Apr 09 2018