Struct initializers as expressions

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 4 07:15:43 PST 2015


On Friday, 4 December 2015 at 11:25:12 UTC, Mike Parker wrote:
> On Friday, 4 December 2015 at 10:42:46 UTC, Marc Schütz wrote:
> ;
>>
>> Then we can add some syntax sugar to leave out the braces, too:
>>
>>     void bar(int a, T t)
>>     bar(42, a: "bla", b: "xyz");
>>
>> This effectively gives us strongly typed named arguments, 
>> without making the names part of the function signature, which 
>> Walter objected to the last time something like this was 
>> proposed.
>
> I like the idea of field names in a struct literal, but I would 
> prefer to keep the parens. And no braces! The syntax for 
> literals is already recommended over the C-style initializers, 
> so IMO the same ought to hold for named initializers. I agree 
> with dropping the struct name, though.
>
> bar(42, (a: "bla", b: "xyz"))
>
> I realized that if named arguments are not supported, then 
> dropping the parens should still indicate that you're dealing 
> with a struct, but the clear delineation is much more obvious. 
> It also holds the door open for Walter to change his mind on 
> named arguments.

Dropping the parens/braces is an optional step, but I would 
prefer to allow it. It looks much cleaner, and from my POV the 
goal is to make it look seamless.

And IIRC Walter isn't really opposed to named arguments per se. 
He just didn't want parameter names to become part of the 
signature. With this proposal, they won't be, and it's opt-in 
from the implementer's POV.


More information about the Digitalmars-d-learn mailing list