Anonymous structs

Era Scarecrow rtcvb32 at yahoo.com
Mon Feb 11 14:20:34 PST 2013


On Monday, 11 February 2013 at 21:30:52 UTC, Jacob Carlborg wrote:
> I've been thinking for this feature for a while. An anonymous 
> struct is basically what it sound like, a struct without a 
> name. The inserting part is its members. If two anonymous 
> structs have the same members (not necessarily in the same 
> order) they are considered to be of the same type. This shows 
> how anonymous structs looks like:
>
> { int x, int y } point = { y: 4, x: 5 };
> auto point2 = { x: 1, y: 2 };
> point = point2;
>
> In this example "point" and "point2" are of the same type since 
> they have the same members. This is basically lowered to 
> something similar to:

  What if there's another anonymous struct that has a little more?

   { int x, int y } point = { y: 4, x: 5 };
   { int x, int y, int color } color_point
           = { y: 4, x: 5, color: 0x000000 };
   //which anonymous struct does it go with?
   //Or can auto only work with named/returned structs?
   auto point2 = { x: 1, y: 2 };

   point = point2; //error, point2 type void _error

  We can instantiate structs with fewer arguments than elements 
they hold. It would either have to go the safe route, or try it's 
best to either be 'safe' or 'whatever seems to work' until you 
change something and break it utterly with no description of 
'what' it is.

  Anonymous structs would be more useful as single instances like 
unions can be in C. (I think it's disallowed in D, or I had 
trouble with them)

Nick Sabalausky wrote:
>Isn't that all basically syntax sugar for tuples?

  Maybe. I would think an explicit tuple would be better.


More information about the Digitalmars-d mailing list