[Issue 4565] In array literals single values can replace arrays of length 1

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 15 00:49:21 PST 2012


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



--- Comment #4 from Kenji Hara <k.hara.pg at gmail.com> 2012-11-15 00:49:19 PST ---
(In reply to comment #0)
> This program compiles with no errors with dmd 2.047:
> 
> int[1][3] a1 = [1, 2, 3];
> void main() {
>     int[1][3] a2 = [1, 2, 3];
> }
>
> But those array literals are wrong. This is the correct program:
> 
> int[1][3] a1 = [[1], [2], [3]];
> void main() {
>     int[1][3] a2 = [[1], [2], [3]];
> }

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]

----

> 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.

-- 
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