DIP 1031--Deprecate Brace-Style Struct Initializers--Community Review Round 1 Discussion
Steven Schveighoffer
schveiguy at gmail.com
Thu Feb 13 16:11:10 UTC 2020
On 2/13/20 10:49 AM, IGotD- wrote:
> On Thursday, 13 February 2020 at 13:05:19 UTC, Steven Schveighoffer wrote:
>>
>> 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.
>>
>
> Isn't that a bit restrictive? Wouldn't you like to have a constructor
> and sometimes parenthesis style initialization? With using parenthesis
> for everything there will be an ambiguity. In that case keeping () for
> constructor and {} for initialization would make it clear what the
> programmer wants.
>
> If the compiler cannot match a constructor it can go on and use
> initialization instead. Would this be too allowing and lead to that
> programmer errors are missed?
You misunderstand.
{} intialization today does not work when a constructor is defined:
struct S
{
int a;
this(int a) {this.a = a; }
}
void main()
{
S s = {a: 1};
}
Error: struct S has constructors, cannot use { initializers }, use S(
initializers ) instead
So the only place this DIP would deprecate valid code is for structs
without ctors.
-Steve
More information about the Digitalmars-d
mailing list