DIP 1031--Deprecate Brace-Style Struct Initializers--Community Review Round 1 Discussion

Petar Petar
Thu Feb 13 10:18:02 UTC 2020


On Thursday, 13 February 2020 at 09:47:13 UTC, IGotD- wrote:
> According to the DIP
>
> This will be deprecated.
>
> struct S { int a, b; }
>
> S s = { 1, 2 }; // Deprecated: use S(1, 2) instead
>
>
> So after this DIP we would write
>
> S s = S(1, 2);
>
> instead.
>
> However, would
>
> S s = (1, 2);
>
> be allowed inferring the type?

This is allowed today:

     auto s = S(1, 2);

This as well:

     alias args = AliasSeq!(1, 2);
     auto s = S(args);
     S s2 = S(args);

If this was accepted:

     alias args = AliasSeq!(1, 2);
     S s = args;

We could also write:

     S s = AliasSeq!(1, 2);

And also, if Timon's proposal [1] is accepted, we would get 
built-in syntax for AliasSeq-es, so we would finally be able to 
write:

     S s = (1, 2);

[1]: 
https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md

>
> Would there be case where this type of initialization would 
> mask the constructor of a type or vice versa?
>
> Ex.
>
> struct S
> {
>     int a, b;
>     this(int bp, int ap)
>     {
>         a = ap;
>         b = bp;
>     }
> }
>
> writing
>
> S s = S(1, 2);
>
> what would it pick?

A user-defined constructor prevents the generation of a 
compiler-synthesized one.

In the code above, the result is `S(2, 1)`, i.e. `1` is assigned 
to `S.a` and `2` to `S.b`. This DIP won't change anything in this 
regard.



More information about the Digitalmars-d mailing list