One case of array assignments

bearophile bearophileHUGS at lycos.com
Mon Mar 11 20:23:51 PDT 2013


A small question relative to this issue (see it for more info):

http://d.puremagic.com/issues/show_bug.cgi?id=4565

Today this syntax compiles:

void main() {
     int[1][3] a2;
     a2[0][] = 1;
     a2[1][] = 2;
     a2[2][] = 3;
}



This used to compile fine, but today it gives warnings (and this 
is good):

void main() {
     int[1][3] a3;
     a3[0] = 1;
     a3[1] = 2;
     a3[2] = 3;
}


test.d(3): Warning: explicit element-wise assignment 
(a3[cast(uint)0])[] = 1 is better than a3[cast(uint)0] = 1
test.d(4): Warning: explicit element-wise assignment 
(a3[cast(uint)1])[] = 2 is better than a3[cast(uint)1] = 2
test.d(5): Warning: explicit element-wise assignment 
(a3[cast(uint)2])[] = 3 is better than a3[cast(uint)2] = 3


Those warnings come from this ER, that asks the [] to be always 
present when you perform an array operation or slice assignment 
(that is a basic array op):

http://d.puremagic.com/issues/show_bug.cgi?id=7444


Currently both of the following forms are accepted:

void main() {
     int[3] a0 = [1, 2, 3];
     int[1][3] a1 = [[1], [2], [3]];
     int[1][3] a2 = [1, 2, 3]; // Second syntax.
}


I don't like the confusion of items with single-item arrays, in 
that second syntax. (Generally in a language muddling the 
semantics or syntax leads to troubles later.)


Also visible here (this compiles):

void main() {
     char[3] a0 = ['a', 'b', 'c'];
     char[3] a1 = "abc";
     char[1][3] a2 = [['a'], ['b'], ['c']];
     char[1][3] a3 = ['a', 'b', 'c']; // Second syntax.
     char[1][3] a4 = "abc"; // Second syntax.
}


In issue 4565 I am suggesting to disallow the second syntax 
because if we are going to deprecate the assignment of array 
slices without using [], then maybe the idea of allowing both of 
those syntaxes is not good.

What do you think? Should be disallow it, or should we close 
issue 4565?

Thank you,
bye,
bearophile


More information about the Digitalmars-d mailing list