www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Anonymous structure

reply Tofu Ninja <emmons0 purdue.edu> writes:
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
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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
next sibling parent Tofu Ninja <emmons0 purdue.edu> writes:
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:
 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.
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.
 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 { } }
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 work
Apr 17 2016
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
next sibling parent Tofu Ninja <emmons0 purdue.edu> writes:
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
prev sibling parent reply Tofu Ninja <emmons0 purdue.edu> writes:
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
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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 with
Yeah, any attribute can be grouped with braces or colons in D, including user-defined ones.
Apr 17 2016
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/17/16 11:57 PM, Tofu Ninja wrote:
 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...
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. -Steve
Apr 18 2016
parent Adrian Matoga <dlang.spam matoga.info> writes:
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
prev sibling parent reply captaindet <2krnk gmx.net> writes:
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
next sibling parent Tofu Ninja <emmons0 purdue.edu> writes:
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
prev sibling parent reply ZombineDev <petar.p.kirov gmail.com> writes:
On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
 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);
There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.com
Apr 19 2016
parent reply Tofu Ninja <emmons0 purdue.edu> writes:
On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:
 On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
 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);
There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.com
How is that supposed to work here?
Apr 19 2016
parent reply ZombineDev <petar.p.kirov gmail.com> writes:
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:
 On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
 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);
There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.com
How is that supposed to work here?
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 }
Apr 19 2016
parent reply ZombineDev <petar.p.kirov gmail.com> writes:
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:
 On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:
 On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
 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);
There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.com
How is that supposed to work here?
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 }
Also functions defined in _b can access members of A.
Apr 19 2016
parent ZombineDev <petar.p.kirov gmail.com> writes:
On Tuesday, 19 April 2016 at 20:19:37 UTC, ZombineDev wrote:
 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:
 On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:
 On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
 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);
There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1 digitalmars.com
How is that supposed to work here?
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 }
Also functions defined in _b can access members of A.
And also: import std.traits; writeln(Fields!A.stringof); // prints () writeln(Fields!Outer.stringof); // prints (Inner, int)
Apr 19 2016