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

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 29 11:08:59 PDT 2014


Steven Schveighoffer:
> On Tue, 15 Apr 2014 13:46:11 -0400, bearophile
>> What do you think are the reasons I suggested the enhancement 
>> for?
>
> To make people write code the way you like? :)
>
> Honestly, it's like you require someone to call a function like:
>
> T foo(T)(T t){return t;}
>
> Just so you can see the foo there. I understand the idea, but 
> the result is not logical, just superficial.

Issue 7444 allows to tell apart the two very different operations 
of this code, that look the same:


void main() {
     int[][3] x;
     int[]    y;
     int[]    z;

     x[] = z; // copies just the z pointer
     y[] = z; // copies the elements in z
}


In Phobos there are awkward names like walkLength, and in D we 
don't have a built-in "x in array" syntax, both just to tell 
apart the O(1) cases from the O(n) ones. Requiring the [] when 
you perform an array copy (instead of just a copy of the slice 
struct) allows to visually tell apart the O(1) operations from 
the O(n) ones:

void main() {
     int[][3] x;
     int[]    y;
     int[]    z;
     x[] = z;
     y[] = z[];
}



> the result is not logical, just superficial.

In D vector operations are allowed only with the [] syntax:

void main() {
     int[] a, b;
     a = a + b;       // Wrong
     a[] = a + b;     // Wrong
     a = a[] + b;     // Wrong
     a = a + b[];     // Wrong
     a[] = a[] + b;   // Wrong
     a = a[] + b[];   // Wrong
     a[] = a[] + b[]; // OK
}

A copy of the items can be seen as the simplest vector operation, 
so I think it's logical for it to require the [] like the others.

Bye,
bearophile


More information about the Digitalmars-d mailing list