Struct Initialization syntax

Cym13 cpicard at openmailbox.org
Mon Jul 23 17:10:08 UTC 2018


On Monday, 23 July 2018 at 16:26:42 UTC, Seb wrote:
> tl;dr: the currently proposed syntax options are:
>
>
> ---
> struct S
> {
>     int a = 2, b = 4, c = 6;
> }
> void foo()
> {
>     bar(S({c: 10})); // Option 1
>     bar(S(c: 10));   // Option 2
>     bar(S{c: 10});   // Option 3
> }
> ---
>
> So the struct-initialization DIP has been stalled for too long 
> and I think it's time we finally get this story done.
> I personally prefer option 2, but this might be in conflict to 
> named arguments which we hopefully see in the near future too. 
> Hence, I'm leaning forward to proposing Option 1 as the 
> recommended Option for the DIP (that's also what the PoC DMD PR 
> implements). What's your take on this?
>
> DIP: https://github.com/dlang/DIPs/pull/71
> Rendered view: 
> https://github.com/wilzbach/DIPs/blob/struct-initialization/DIPs/DIP1xxx-sw.md

I'm in favour of 3.

Option 2 looks nice but I'm against it because of possible named 
arguments. Even though they're not part of the language yet and 
may never be we already have too many clunky things not to avoid 
a conflict when we can.

Option 1 is clean but a bit strange, I don't like the idea of 
doubling the enclosing symbols, in that situation you'd expect 
S() and {} to have separate effects, not to combine into a 
special effect. It also looks like a constructor while it's not, 
which isn't a nice conflation to make. I wouldn't be very 
dismayed by it though.

Still, that's why I prefer option 3 which is very similar to 
classical struct initialization and has clearly only one effect.

PS: Now that I think about it, would something like S{c:3}("a") 
be allowed to say “Call the constructor with the string "a" as 
argument on the struct of type S initialized with c=3”? I may 
have missed it but I don't think that's addressed by the DIP.


More information about the Digitalmars-d mailing list