www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array copy warning

reply Benjamin Thaut <code benjamin-thaut.de> writes:
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 
mem[cast(uint)0..this.length()] = (this.opSlice())[] is better than 
mem[cast(uint)0..this.length()] = this.opSlice()

I don't quite understand this warning. It doesn't make much sense to 
overload the slice operator if I can't use it the same way as the 
regular slice operator on a "normal" array. Whats the idea behind this 
warning?
-- 
Kind Regards
Benjamin Thaut
Oct 13 2013
next sibling parent reply Benjamin Thaut <code benjamin-thaut.de> writes:
Am 13.10.2013 15:10, schrieb 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
 mem[cast(uint)0..this.length()] = (this.opSlice())[] is better than
 mem[cast(uint)0..this.length()] = this.opSlice()

 I don't quite understand this warning. It doesn't make much sense to
 overload the slice operator if I can't use it the same way as the
 regular slice operator on a "normal" array. Whats the idea behind this
 warning?
Additionally this warning appears in generic code. Which means I don't even now if a regular array is passed in as a argument or just a type which overloads the slice operator. -- Kind Regards Benjamin Thaut
Oct 13 2013
parent reply Mike Wey <mike-wey example.com> writes:
On 10/13/2013 03:15 PM, Benjamin Thaut wrote:
 Am 13.10.2013 15:10, schrieb 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
 mem[cast(uint)0..this.length()] = (this.opSlice())[] is better than
 mem[cast(uint)0..this.length()] = this.opSlice()

 I don't quite understand this warning. It doesn't make much sense to
 overload the slice operator if I can't use it the same way as the
 regular slice operator on a "normal" array. Whats the idea behind this
 warning?
Additionally this warning appears in generic code. Which means I don't even now if a regular array is passed in as a argument or just a type which overloads the slice operator.
With 2.063 and up when assigning to a slice you'll also need to use the slice operator on the right of the assignment. This is also true for regular arrays. -- Mike Wey
Oct 13 2013
parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 13.10.2013 17:22, schrieb Mike Wey:
 With 2.063 and up when assigning to a slice you'll also need to use the
 slice operator on the right of the assignment.
 This is also true for regular arrays.
The code that generates the warning looks like this: mem[0..this.length] = this[]; So I assume this already correct, unless I misunderstand it and the new form should be: mem[0..this.length] = this[][];
Oct 13 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Oct 15 2013