[DIP] In-place struct initialization

Georgi D via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 11 11:54:45 PDT 2016


On Sunday, 31 July 2016 at 07:10:46 UTC, cym13 wrote:
> On Sunday, 31 July 2016 at 04:55:31 UTC, deadalnix wrote:
>> On Saturday, 30 July 2016 at 21:42:42 UTC, cym13 wrote:
>>>[...]
>>
>> That doesn't help. In fact, it makes things worse as now 
>> constructor calls and function call do not have the same 
>> syntax. You've just created an holly mess in the parser.
>>
>> If something we should strive to get constructor call more 
>> like regular function call rather than less (for instance by 
>> behaving the same way when it comes to IFTI).
>>
>> It is also unclear how overload resolution is supposed to work.
>>
>> If I may suggest one thing it is to not start with the 
>> intended result for the DIP, but start from the intended 
>> change int he language, then, and only then, examine what 
>> comes out of it.
>
> I don't understand this comment. This isn't about construction, 
> it's about initialization, the call can occur only at one 
> precise time and no there is no overload concern as there is no 
> function call. The proposed change is litteraly just equivalent 
> to the already existing struct initialization, just made usable:
>
>     struct S {
>         int a;
>         int b;
>     }
>
>     auto s = S(b:42);
>     // equivalent to
>     S s = { b:42 };
>
> Having the possibility to initialize structs without tying them 
> to a variable
> proves useful when combined with functions that take this 
> struct but those
> functions aren't directly impacted by the change.

I think a feature like this would be very useful especially for 
User Defined Attributes where it is not possible to write:

S s = {b:42}

Being able to write:

@S(b:42) void foo();

would be great. I do not think there is another solution for this 
at the moment.

I am fine with using curly braces as well if it makes the grammar 
more clean.

@S{b:42} void foo();


More information about the Digitalmars-d mailing list