[DIP] In-place struct initialization

Patrick Schluter via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 31 10:01:10 PDT 2016


On Sunday, 31 July 2016 at 14:38:33 UTC, Lodovico Giaretta wrote:
> On Sunday, 31 July 2016 at 13:39:58 UTC, Enamex wrote:
>> I suggest extending the existing `S s = {field: value}` syntax 
>> to allow specifying the type itself next to the field list and 
>> make it usable generally everywhere.
>>
>> So, instead:
>>
>> takeThing(Thing{ field: val, num: 43 });
>>
>> It shouldn't clash with anything else, I think.
>
> I support this idea of extending curly-brace initializers. It 
> would be very useful and less ambiguous than parenthesized 
> initializers.
>
> [A thread about this]
> http://forum.dlang.org/post/ni0u47$2100$1@digitalmars.com
>
> [An issue about this]
> https://issues.dlang.org/show_bug.cgi?id=15692

It's strange that D wouldn't support something like that as even 
C (C99) can do it with compound literals (struct s){ .z = "Pi", 
.x = 3, .y = 3.1415 }. It's absolutely possible to pass it to a 
function taking a struct s. You can even take its address with & 
if the fonction take a pointer to a struct. I use it all the time 
on my work project.




More information about the Digitalmars-d mailing list