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

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 15 08:59:31 PDT 2014


On Tue, 15 Apr 2014 11:47:24 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Andrei Alexandrescu:
>
>> This doesn't quite explain much. -- Andrei
>
> Look at the issue:
> https://issues.dlang.org/show_bug.cgi?id=7444

I agree with Andrei, and the rejection of the change.

On the left hand side, the [] operator is special *for arrays* in that it  
denotes you want to do an element-wise copy. On the right hand side, this  
is not the case, [] is simply an operator. It's a no-op if the rhs is an  
array, since [] just gets the array again.

Requiring it simply adds unneeded hoops through which you must jump, the  
left hand side denotes the operation, the right hand side does not, and in  
fact, this causes problems with types that overload [] to return an array  
(a very logical operation).

In particular, this would case issues with dcollections' ArrayList which  
returns a slice when doing []. The other example from Benjamin is  
essentially the same thing.

Note -- it would be nice (and more consistent IMO) if arr[] = range worked  
identically to arr[] = arr.

-Steve


More information about the Digitalmars-d mailing list