Differing semantics between multidimensional fixed-length array and slice initialization

Ali Çehreli acehreli at yahoo.com
Sun Mar 31 21:54:37 PDT 2013


On 03/31/2013 06:36 PM, Nicholas Smith wrote:> Hello there,
 >
 > So I noticed something about the semantics of multidimensional
 > fixed-length array vs slice initialization:
 >
 > Code:
 >
 > int[2][3] array;

Obviously, like C and C++, D does not have multi-dimensional arrays but 
D's array of array syntax is consistent.

When we say

   A[3] a;

we mean three As. Replacing A with an array gives us this:

   int[2][3] a;

Now we mean three int[2]s. This is consistent, because 'int[2]' in there 
itself follows the same syntax.

 > writeln(array.length, " ", array[0].length);
 > int[][] slice = new int[][](2, 3);

That is very similar to a function API. It would make sense that the 
outer-most index came first if it truly were a function.

 > writeln(slice.length, " ", slice[0].length);
 >
 > Output:
 >
 > 3 2
 > 2 3
 >
 > So it seems that int[2][3] means "an array of 3 int arrays of length 2",
 > while new int[][](2, 3) means "an array of 2 int arrays of length 3".
 >
 > This seems like a direct conflict of semantics, and I was wondering
 > whether this was an intentional design choice, or an oversight. Can
 > anyone explain reasoning behind this?

Ali



More information about the Digitalmars-d-learn mailing list