On Thursday, 4 July 2013 at 23:02:10 UTC, Jonathan M Davis wrote:
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*).
- Jonathan M Davis
Arguably, this all comes from C's "value centric" scheme. When
you write (not the C-like placement of the *):
int *p, a;
It means: with *p and a, you get an int.
Ditto, when you write:
int arr[5];
It is written that way to reflect that usage is:
*p = arr[0];
As stated in the first question, it makes sense that way in the
context of:
#define WIDTH = 10
#define HEIGHT = 5
int matrix[WIDTH][HEIGHT];
a = matrix[w][h];
I'm just saying that's the explanation for it. Once you have to
start making the distinction between "array of pointers" and
"pointer to array", then clearly, things start crumbling apart,
and D's approach is simply better. I'm just saying, there *is* an
historical reason for this, not just arbitrary stupidness.
The fact that in D, declaration and indexing is "reversed" is
something I've truly never even noticed, since to me, naturally,
declarations read right to left, then parsing is left to right.
So no problem there.