Anonymous structs

Jacob Carlborg doob at me.com
Mon Feb 11 23:51:52 PST 2013


On 2013-02-11 23:58, MattCoder wrote:
> On Monday, 11 February 2013 at 21:30:52 UTC, Jacob Carlborg wrote:
>> The advantage of anonymous structs is that they can be declared in
>> place, in function declartions for example:
>>
>> void foo ({ int x, int y } point)
>> {
>> }
>>
>> foo({ y: 5, x: 3 });
>
> At the first look it seems interesting, but imagine that you need to
> change one type or add more members to that struct, have you imagined
> the mess to change all those declarations?

If you add a new member to the anonymous struct declared in "foo" any 
existing call will still match. That's just like the current initializer 
syntax works:

struct Bar
{
     int a;
     int b;
}

Bar bar = { b: }; // "a" is default initialized

If you remove a member from the anonymous struct declared in "foo" you 
will get a compile error since there will be a member that doesn't match.

> Because that case I prefer the old way:
>
> void foo(MyPointStruct point)
> {
> }
>
> Any changing in "MyPointStruct" will be consumed by all the code.

I'm not sure I see the difference compared with the anonymous struct.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list