Not sure if actual Bug or ER.
Given a struct containing a static array, and initializing the *members* of that static array:
//----
struct S
{
this(this){writeln("postblit");}
void opAssign(ref S other){writeln("opAssign");}
}
struct Bar
{
S[3] value;
this(S s)
{
writeln("begin");
value[0] = s; //init
value[1] = s; //assign
value[2] = s; //assign
writeln("end");
}
}
void main()
{
auto bar = Bar(S.init);
}
//----
Produces:
//----
postblit
opAssign
opAssign
//----
This means the compiler "sees" the first assignement is actually initialization. However, for the subsequent "=", it doesn't.
This is probably because "value" is already tagged as initialized. I think there should be a better granularity for that (single member granularity).
Also, for example:
//----
struct S
{
const(int[2]) value;
this(int)
{
value[0] = 0;
value[1] = 1; //HERE
}
}
//----
Error: multiple field value initialization
//----
I think such code should be valid initialization.
Comment #1 by k.hara.pg — 2013-12-14T21:55:22Z
(In reply to comment #0)
> Not sure if actual Bug or ER.
This is intended behavior, so this should be marked as enhancement.
This behavior is introduced by:
Issue 9665 - Structure constant members can not be initialized if have opAssign
Related regressions which they were rejected:
Issue 11258 - Static field access problem
Issue 11343 - [2.064 beta] Error: multiple field initialization
Issue 11346 - [2.064 beta] field initializing not allowed in loops or after labels
Comment #2 by monarchdodra — 2013-12-15T00:39:19Z
(In reply to comment #1)
> (In reply to comment #0)
> > Not sure if actual Bug or ER.
>
> This is intended behavior, so this should be marked as enhancement.
Enhancement works for me.
> This behavior is introduced by:
> Issue 9665 - Structure constant members can not be initialized if have opAssign
Yes. That is very good behavior, thank you.
> Related regressions which they were rejected:
> Issue 11258 - Static field access problem
> Issue 11343 - [2.064 beta] Error: multiple field initialization
> Issue 11346 - [2.064 beta] field initializing not allowed in loops or after
> labels
I think these mostly asked for the ability to initialized the same variable multiple times (which I also think is wrong).
I'm asking for the compiler to try to view static arrays as a aggregate of values:
You can either initialize the entire array at once (in which case, the entire array would be considered initialized, as well as each member), or each element at once, in which case each element would be initialized individually 1 by 1.
Use cases:
this(int)
{
value[0] = s; //init
value[1] = s; //init
value[0] = s; //assign
}
this(int)
{
value = s; //init all
value[0] = s; //assign
}
this(int)
{
value[0] = s; //init
//value already written to
value = s; //assign to all
value[1] = s; //assign
}
Comment #3 by robert.schadek — 2024-12-13T18:15:15Z