Allow designated initialization of struct

ryuukk_ ryuukk.dev at gmail.com
Fri Sep 13 14:18:28 UTC 2024


On Friday, 13 September 2024 at 10:55:11 UTC, Nick Treleaven 
wrote:
> On Thursday, 12 September 2024 at 12:14:17 UTC, ryuukk_ wrote:
>> ```D
>> struct Test
>> {
>>     int test;
>> }
>>
>> struct Data
>> {
>>     Test[8] test;
>> }
>>
>> void add(Data data)
>> {
>> }
>>
>> extern(C) void main()
>> {
>>     add( Data(test: [ {test: 1}] ) );
>> }
>>
>> ```
>
> You are passing a struct literal to `add`. A struct literal is 
> an expression:
> https://dlang.org/spec/struct.html#struct-literal
>
>> A struct literal consists of the name of the struct followed 
>> by a parenthesized named argument list
>
> So `[ {test: 1}]` is parsed as an array literal (because it is 
> an expression), not an array intializer. `{test: 1}` in 
> expression context is parsed as an invalid function literal 
> (because there's no semi-colon).

This should be allowed, another DIP idea, if anyone still care 
about improving the language


More information about the dip.ideas mailing list