Multidimensional array

Jonathan M Davis jmdavisProg at gmx.com
Thu Jul 4 16:01:52 PDT 2013


On Friday, July 05, 2013 00:39:47 Oleksiy wrote:
> Hi,
> 
> I'm new to the language and would appreciate if anybody could
> clarify the following:
> 
> 1. What is the rationale behind "prefix declaration" of an array?
> Using right-to-left order to declare an array and left-to-right
> order to access elements seems confusing.

Because the brackets are part of the type, and the type goes to the left of 
the variable name. The fact that C put in on the right-hand side is actually 
quite bizarre, but they also did nonsense like make it
so that in this declaration

int* p1, p2;

p1 is an int*, whereas p2 is an int, which D fixed (in D, both are int*).

Now, the downside to putting them on the left is the order of the sizes. Types 
are read outwards from variable name. This becomes particularly important when 
you try and understand stuff like C function pointer declarations, since the 
name ends up in the middle of the declaration. Take

int* i;

for example. It's a pointer to an int, not an int to a pointer. It's read 
right-to-left. Static array declarations are doing the same thing.

int[4][5] arr;

It's a static array of length 5 which holds static arrays of length 4 which 
hold ints. So, the ordering is completely consistent with how the rest of the 
type system works. Reading it from left-to-right would be inconsistent, much 
as most people tend to think of types that way.

Now, we've already broken that rule in it least one case - const and 
immutable. In C and C++,

const int* p;

and

int const* p;

are identical, and technically, the second one would be more correct - because 
it gives you pointer to a const int, not a pointer to an int const. But the 
first one is allowed and what is frequently used, if nothing else, because 
people tend to try and read type declarations left-to-right.

In D, however, we have

const int* p;

and

const(int*) p;

but no

int const* p;

So, in this case, we arguably already broke the right-to-left rule. As such, 
it arguably would have been better to also break that rule with regards to 
static arrays, but the decision on static arrays far predates even adding 
const into the language, so at the time static arrays were introduced, making 
them read left-to-right would have been inconsistent with everything else, and 
while const is now inconsistent with everything else, most everyone is used to 
writing const on the left anyway, so I doubt that much of anyone thought about 
it being inconsistent when it was added.

The whole "read the type outward from the variable name" deal is not as 
critical in D as it is in C/C++, because we don't declare function pointers in 
the same way (which is where it's truly critical in C/C++), but we inherited 
it from C/C++, and for the most part, D still follows it.

Fixing it so that the sizes for static arrays were read left-to-right would 
definitely be a usability improvement, but even if we were to decide that the 
whole "read the type outward from the variable name" was unimportant enough to 
make the change desirable, it would silently break all kinds of code at this 
point, so we're stuck with it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list