digitalmars.D.learn - Anonymous structure
- Tofu Ninja (25/25) Apr 17 2016 Just out of curiosity, what is the point of the following?
- Nicholas Wilson (9/34) Apr 17 2016 Try adding static:
- Tofu Ninja (21/60) Apr 17 2016 It does, it compiles...
- Adam D. Ruppe (2/3) Apr 17 2016 They are allowed only if they are inside another aggregate.
- Adam D. Ruppe (17/25) Apr 17 2016 The grouping matters when it is nested inside a union. Here's a
- Tofu Ninja (4/6) Apr 17 2016 I understand the reason for allowing it in a union, I just don't
- Tofu Ninja (26/28) Apr 17 2016 Actually curiously I found another potential use, applying
- Adam D. Ruppe (3/6) Apr 17 2016 Yeah, any attribute can be grouped with braces or colons in D,
- Steven Schveighoffer (13/40) Apr 18 2016 I wonder if it makes a difference for layout. So for example:
- Adrian Matoga (3/14) Apr 19 2016 There isn't. T.init.z.offsetof - T.init.y.offsetof == 1.
- captaindet (17/26) Apr 18 2016 not sure what you mean by "named substructure, not a nested structure"
- Tofu Ninja (4/20) Apr 19 2016 Yeah thats basicly what I meant, just sort of tedious to have to
- ZombineDev (3/31) Apr 19 2016 There's another way:
- Tofu Ninja (2/37) Apr 19 2016 How is that supposed to work here?
- ZombineDev (18/56) Apr 19 2016 struct A
- ZombineDev (2/59) Apr 19 2016 Also functions defined in _b can access members of A.
- ZombineDev (5/67) Apr 19 2016 And also:
Just out of curiosity, what is the point of the following? struct a{ struct{ int x; int y; int z; } } As far as I can tell, the anonymous structure does nothing. How is it different from struct a{ int x; int y; int z; } Also is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }
Apr 17 2016
On Monday, 18 April 2016 at 02:12:24 UTC, Tofu Ninja wrote:Just out of curiosity, what is the point of the following? struct a{ struct{ int x; int y; int z; } } As far as I can tell, the anonymous structure does nothing. How is it different from struct a{ int x; int y; int z; }IIRC D doesn't allow anonymous structures.Also is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }Try adding static: struct a { static struct b { } }
Apr 17 2016
On Monday, 18 April 2016 at 02:42:15 UTC, Nicholas Wilson wrote:On Monday, 18 April 2016 at 02:12:24 UTC, Tofu Ninja wrote:It does, it compiles... Accessing x,y,z on the first one with the anonymous struct is the same as accessing it on the second without the anonymous struct... Seems to make no difference that it is there, which is why I am asking.Just out of curiosity, what is the point of the following? struct a{ struct{ int x; int y; int z; } } As far as I can tell, the anonymous structure does nothing. How is it different from struct a{ int x; int y; int z; }IIRC D doesn't allow anonymous structures.Does not seem to be what I mean, a static nested struct is just a nested struct without access to the enclosing structure's members. What I meant was a struct to just add a namespace of sorts to the struct so that the substructure members would have to be accessed with a longer more qualified name. Something like struct a{ int x; sub_struct b{ int y; } } a v; v.x = 3; v.b.y = 7; // v.y = 7; // does not workAlso is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }Try adding static: struct a { static struct b { } }
Apr 17 2016
On Monday, 18 April 2016 at 02:42:15 UTC, Nicholas Wilson wrote:IIRC D doesn't allow anonymous structures.They are allowed only if they are inside another aggregate.
Apr 17 2016
On Monday, 18 April 2016 at 02:12:24 UTC, Tofu Ninja wrote:Just out of curiosity, what is the point of the following? struct a{ struct{ int x; int y; int z; } }The grouping matters when it is nested inside a union. Here's a real world example: https://github.com/adamdruppe/arsd/blob/master/color.d#L128 The anonymous struct inside the union allows me to say that r,g,b, and a are NOT supposed to share memory, but rather give structure to the shared uint or ubyte[4] in the other union members. My documentation generator also uses the grouping to add shared docs for the innards: http://dpldocs.info/experimental-docs/arsd.color.Color.__anonymous.html (I'm not terribly happy with giving it the name `__anonymous` in the docs but I didn't have any better idea yet.) Though that isn't a feature of D itself, I do find it nice to be able to group documentation. The struct inside union is the main pure-language use case I know of though.
Apr 17 2016
On Monday, 18 April 2016 at 03:33:53 UTC, Adam D. Ruppe wrote:The struct inside union is the main pure-language use case I know of though.I understand the reason for allowing it in a union, I just don't see the reason it was extended to all aggregates as it seems to do nothing.
Apr 17 2016
On Monday, 18 April 2016 at 03:33:53 UTC, Adam D. Ruppe wrote:The struct inside union is the main pure-language use case I know of though.Actually curiously I found another potential use, applying attributes/UDAs to multiple members at once. enum testUDA; struct T{ testUDA immutable struct{ int x; int y; int z; } } x,y,and z seem to all be immutable and all have the UDA testUDA. But even odder, it seems that "struct" in there is doing absolutely nothing. The same thing can be done with enum testUDA; struct T{ testUDA immutable{ int x; int y; int z; } } So it still seems to be useless other than in the case of unions...
Apr 17 2016
On Monday, 18 April 2016 at 03:57:26 UTC, Tofu Ninja wrote:x,y,and z seem to all be immutable and all have the UDA testUDA. But even odder, it seems that "struct" in there is doing absolutely nothing. The same thing can be done withYeah, any attribute can be grouped with braces or colons in D, including user-defined ones.
Apr 17 2016
On 4/17/16 11:57 PM, Tofu Ninja wrote:On Monday, 18 April 2016 at 03:33:53 UTC, Adam D. Ruppe wrote:I wonder if it makes a difference for layout. So for example: struct T { struct { int x; ubyte y; } ubyte z; } If there is padding inserted between y and z. -SteveThe struct inside union is the main pure-language use case I know of though.Actually curiously I found another potential use, applying attributes/UDAs to multiple members at once. enum testUDA; struct T{ testUDA immutable struct{ int x; int y; int z; } } x,y,and z seem to all be immutable and all have the UDA testUDA. But even odder, it seems that "struct" in there is doing absolutely nothing. The same thing can be done with enum testUDA; struct T{ testUDA immutable{ int x; int y; int z; } } So it still seems to be useless other than in the case of unions...
Apr 18 2016
On Monday, 18 April 2016 at 15:59:11 UTC, Steven Schveighoffer wrote:I wonder if it makes a difference for layout. So for example: struct T { struct { int x; ubyte y; } ubyte z; } If there is padding inserted between y and z.There isn't. T.init.z.offsetof - T.init.y.offsetof == 1.
Apr 19 2016
On 2016-04-18 14:12, Tofu Ninja wrote:Also is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);
Apr 18 2016
On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);Yeah thats basicly what I meant, just sort of tedious to have to write it like that, makes more complex layouts a real pain to write.
Apr 19 2016
On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:On 2016-04-18 14:12, Tofu Ninja wrote:There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.comAlso is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);
Apr 19 2016
On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:How is that supposed to work here?On 2016-04-18 14:12, Tofu Ninja wrote:There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.comAlso is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);
Apr 19 2016
On Tuesday, 19 April 2016 at 17:16:00 UTC, Tofu Ninja wrote:On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:struct A { template _b() { int x, y, z; } alias b = _b!(); } void main() { import std.stdio; auto a = A(); a.b.x = 5; writeln(a.b.x); // prints 5 //writeln(a.b); // Error: expression has no value }On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:How is that supposed to work here?On 2016-04-18 14:12, Tofu Ninja wrote:There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.comAlso is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);
Apr 19 2016
On Tuesday, 19 April 2016 at 20:18:07 UTC, ZombineDev wrote:On Tuesday, 19 April 2016 at 17:16:00 UTC, Tofu Ninja wrote:Also functions defined in _b can access members of A.On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:struct A { template _b() { int x, y, z; } alias b = _b!(); } void main() { import std.stdio; auto a = A(); a.b.x = 5; writeln(a.b.x); // prints 5 //writeln(a.b); // Error: expression has no value }On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:How is that supposed to work here?On 2016-04-18 14:12, Tofu Ninja wrote:There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.comAlso is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);
Apr 19 2016
On Tuesday, 19 April 2016 at 20:19:37 UTC, ZombineDev wrote:On Tuesday, 19 April 2016 at 20:18:07 UTC, ZombineDev wrote:And also: import std.traits; writeln(Fields!A.stringof); // prints () writeln(Fields!Outer.stringof); // prints (Inner, int)On Tuesday, 19 April 2016 at 17:16:00 UTC, Tofu Ninja wrote:Also functions defined in _b can access members of A.On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:struct A { template _b() { int x, y, z; } alias b = _b!(); } void main() { import std.stdio; auto a = A(); a.b.x = 5; writeln(a.b.x); // prints 5 //writeln(a.b); // Error: expression has no value }On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:How is that supposed to work here?On 2016-04-18 14:12, Tofu Ninja wrote:There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.comAlso is there a way to have a named substructure, not a nested structure but something to just add an additional name, maybe something like struct a{ struct{ int x; int y; int z; } b; }not sure what you mean by "named substructure, not a nested structure" but this works: struct Outer{ struct Inner{ int x; int y; int z; } Inner inner; int a; } Outer outer; outer.a = 7; outer.inner.y = 42; // outer.x = 13; //fails writeln(outer);
Apr 19 2016