Syntax sugar for {} with structs

ryuukk_ ryuukk.dev at gmail.com
Fri Jul 1 19:21:28 UTC 2022


On Friday, 1 July 2022 at 18:54:24 UTC, IGotD- wrote:
> On Friday, 1 July 2022 at 11:51:57 UTC, ryuukk_ wrote:
>> I had to write this recently
>>
>>
>> ```D
>>     void reset()
>>     {
>>         state.end_index = 0;
>>         state.buffer_list = SinglyLinkedList!(ubyte[])();
>>     }
>> ```
>>
>> While not that bad, it's annoying, why can't we do like like:  
>> `A a = {}`?
>>
>>
>> ```D
>>     void reset()
>>     {
>>         state.end_index = 0;
>>         state.buffer_list = {};
>>     }
>> ```
>>
>
> Are you suggesting that D should copy the modern C++ uniform 
> initialization? That's one thing I don't like with C++ because 
> it is very ugly.

I am not suggesting to copy C++, i had to write that code and i 
thought what if we could shorten this and reuse what we do for `A 
a = {};`

> D also don't need it because D has default initialization as 
> standard. C++ added the uniform initialization (among other 
> things) in order to create a syntax where variables are 
> properly initialized. D got this right from the beginning.
>
> In D the .init property is usually fine for most cases.

That's exactly why i came up with that proposal, because in some 
case, things can become very verbose, what if it was a super long 
template?

Is that syntax not good? maybe `#init` `@init`?



More information about the Digitalmars-d mailing list