Jump to page: 1 2
Thread overview
Anonymous structure
Apr 18, 2016
Tofu Ninja
Apr 18, 2016
Nicholas Wilson
Apr 18, 2016
Tofu Ninja
Apr 18, 2016
Adam D. Ruppe
Apr 18, 2016
Adam D. Ruppe
Apr 18, 2016
Tofu Ninja
Apr 18, 2016
Tofu Ninja
Apr 18, 2016
Adam D. Ruppe
Apr 19, 2016
Adrian Matoga
Apr 18, 2016
captaindet
Apr 19, 2016
Tofu Ninja
Apr 19, 2016
ZombineDev
Apr 19, 2016
Tofu Ninja
Apr 19, 2016
ZombineDev
Apr 19, 2016
ZombineDev
Apr 19, 2016
ZombineDev
April 18, 2016
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;
}

April 18, 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
    {
    }
}

April 18, 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:
>> 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
April 18, 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.
April 18, 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.
April 18, 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.
April 18, 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...
April 18, 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 with


Yeah, any attribute can be grouped with braces or colons in D, including user-defined ones.
April 18, 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:
>> 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
April 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);

« First   ‹ Prev
1 2