What's the deal with "Warning: explicit element-wise assignment..."

monarch_dodra via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 15 04:24:36 PDT 2014


On Tuesday, 15 April 2014 at 10:38:06 UTC, Dicebot wrote:
> On Tuesday, 15 April 2014 at 06:31:02 UTC, Andrei Alexandrescu 
> wrote:
>> We've just enabled warnings as errors in our build system at 
>> work and suddenly:
>>
>> Warning: explicit element-wise assignment <exprnew> is better 
>> than <expr>
>>
>> where <exprnew> and <expr> are expressions picked from the 
>> code. For example, this wasn't accepted:
>>
>> writeAvail_[0 .. buf.length] = buf;
>>
>> but this was:
>>
>> writeAvail_[0 .. buf.length] = buf[];
>>
>> The type of the involved variables were as trivial as ubyte[] 
>> and in ubyte[] respectively.
>>
>> What's the idea?
>>
>>
>> Andrei
>
> It is mostly a style warning but greatly helps to reduce 
> reading ambiguity:
>
> // "assign rvalue to lvalue element-wise"
> // OR
> // "assign rvalue to all members of lvalue"
> // need to know rvalue type to be sure
> lvalue[] = rvalue;
>
> // clear and simple
> lvalue[] = rvalue[];

Too bad the warning doesn't work both ways, when using a
"gratuitous" []:

void main()
{
      int[][] a = [[], [], []];
      int[] b = [1, 2];

      //"assign rvalue to lvalue element-by-element" ?
      a[] = b[];
}


More information about the Digitalmars-d mailing list