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

Steven Schveighoffer schveiguy at gmail.com
Thu Feb 13 13:05:19 UTC 2020


On 2/13/20 4:59 AM, Mike Parker wrote:
> On Thursday, 13 February 2020 at 09:56:27 UTC, Walter Bright wrote:
>>
>>> Would there be case where this type of initialization would mask the 
>>> constructor of a type or vice versa?
>>
>> The constructor takes precedence.
>>
>>> struct S
>>> {
>>>      int a, b;
>>>      this(int bp, int ap)
>>>      {
>>>          a = ap;
>>>          b = bp;
>>>      }
>>> }
>>>
>>> writing
>>>
>>> S s = S(1, 2);
>>>
>>> what would it pick?
>>
>> The constructor.
> 
> Which, for the record, is the current behavior. Unfortunately, the 
> struct literal documentation only mentions opCall. I'll submit a PR.
> 
> https://dlang.org/spec/struct.html#struct-literal

That page is *really* old. I think what Mike meant to say is that struct 
initializers and struct literals are both disabled once a constructor or 
opCall function is present.

So in essence, the above struct S could NEVER be initialized except via 
constructor or with it's .init value. Having the named parameter DIP 
accepted means that now the constructor becomes possible to call like 
the initializer, assuming you gave bp and ap default values.

-Steve


More information about the Digitalmars-d mailing list