[Issue 4565] In array literals single values can replace arrays of length 1
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Mar 11 19:44:16 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4565
--- Comment #6 from bearophile_hugs at eml.cc 2013-03-11 19:44:12 PDT ---
(In reply to comment #4)
Sorry for the very delayed answer.
> > A sloppy syntax is bad because it *always* offers space for bugs, like this
> > one, dmd compiles this program with no errors (note the missing comma):
> >
> > int[1][3] a = [[1] [0], [2]];
> > void main() {}
>
> [1][0] is an expression which indexing array literal, and evaluated to 1.
> It's equivalent to: [1,2,3][0] == 1
>
> > Now a contains [1, 2, 0], a silent bug.
>
> Now a is initialized by [1, [2]], and is same as:
> int[1][3] a = void;
> a[0][] = 1;
> a[1] = [2];
> a[2][] = 0; // == int.init
>
> After all, a == [1, 2, 0]. There is no bug.
In my opinion that's probably a bug in user code. I doubt the user meant to
write that. Most probably the user meant to write this, but missed a comma
after the first sub-array:
int[1][3] a = [[1], [0], [2]];
void main() {}
But this is an uncommon situation, so I think it's not worth thinking too much
about it.
What follows is more important:
> I think this is not a bad program.
> > int[1][3] a1 = [1, 2, 3];
> is same as:
>
> int[1][3] a1 = void;
> a1[0][] = 1; // fill all elements by 1
> a1[1][] = 2; // fill all elements by 2
> a1[2][] = 3; // fill all elements by 3
>
> Then a1 is initialized by [[1], [2], [3]].
>
> And it is consistent with:
> int[3] sa = 1; // sa is initialized to [1, 1, 1]
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;
}
temp.d(3): Warning: explicit element-wise assignment (a3[cast(uint)0])[] = 1 is
better than a3[cast(uint)0] = 1
temp.d(4): Warning: explicit element-wise assignment (a3[cast(uint)1])[] = 2 is
better than a3[cast(uint)1] = 2
temp.d(5): Warning: explicit element-wise assignment (a3[cast(uint)2])[] = 3 is
better than a3[cast(uint)2] = 3
Currently both of the following forms are accepted:
void main() {
int[1][3] a1 = [[1], [2], [3]];
int[1][3] a2 = [1, 2, 3];
}
Generally I trust your good judgement Hara, but if we are going to deprecate
the assignment of array slices without using [], then I don't know if the idea
of allowing both of those syntaxes is a good idea...
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list