Array copy warning

bearophile bearophileHUGS at lycos.com
Tue Oct 15 01:57:38 PDT 2013


Benjamin Thaut:

> I'm just in the progress of upgrading to dmd 2.063 and I now 
> get the following warnings in my code:
>
>  Warning: explicit element-wise assignment

I am having problems with code like this, that used to work:


import std.algorithm: reduce;
struct Foo { int x, y; }
void main() {
     Foo[] data = [{10, 20}, {30, 40}];
     reduce!((a, b) => a[] += [b.x, b.y][])([0, 0], data);
}


Now it gives:

test.d(5): Error: invalid array operation a[] += [b.x, b.y] (did 
you forget a [] ?)
...\dmd2\src\phobos\std\algorithm.d(763): Error: template 
instance test.main.__lambda1!(int[], Foo) error instantiating
test.d(5):        instantiated from here: reduce!(int[], Foo[])
test.d(5): Error: template instance test.main.reduce!((a, b) => 
a[] += [b.x, b.y][]).reduce!(int[], Foo[]) error instantiating



Expanding the lambda doesn't fully solve the problem:

import std.algorithm: reduce;
struct Foo { int x, y; }
void main() {
     Foo[] data = [{10, 20}, {30, 40}];
     reduce!((a, b) { int[2] c = [b.x, b.y];
                      a[] += c[];
                      return c;})([0, 0], data);
}


Gives:

...\dmd2\src\phobos\std\algorithm.d(763): Warning: explicit slice 
assignment result = (__lambda1(result, front(_param_1)))[] is 
better than result = __lambda1(result, front(_param_1))


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list