On Friday, January 31, 2020 5:43:44 AM MST MoonlightSentinel via 
Digitalmars-d-learn wrote:
> On Friday, 31 January 2020 at 12:37:43 UTC, Adnan wrote:
> > What's causing this?
>
> You mixed up the array lengths:
>
> const int[3][2] matA = [[0, -1, 2], [4, 11, 2]];
> const int[2][3] matB = [[3, -1], [1, 2], [6, 1]];
>
> matA is an SA containing <2> elements of type int[3].
> matB is an SA containing <3> elements of type int[2].

Specifically, the dimensions are read outwards from the variable name, so on
the left-hand side, that means that they go right-to-left, whereas on the
right-hand side, they go left-to-right. This is consistent with how it works
with types in C/C++ except that there, they put the dimensions for static
arrays on the right-hand side of the variable name, meaning that while you
have to read stuff like pointer types from left-to-right in C/C++, you don't
have to do that with static arrays. Ultimately, what D is doing is
consistent but confusing.

e.g. For C/C++

int** foo; // A pointer to a pointer to an int
int foo[5][2]; // A 5 dimensional array of two dimensional arrays of int
foo[4][1] = 7;

and for D:

int** foo; // A pointer to a pointer to an int
int[2][5] foo; // A 5 dimensional array of two dimensional arrays of int
foo[4][1] = 7;

For C/C++, you often don't realize how the rules work until you have to read
function pointers, because they put the static array lengths no the
right-hand side, and they actually allow you to put stuff like const in
multiple places instead of only in the place where it would be right
right-to-left. e.g. if the rule were followed strictly,

const int i = 0;

wouldn't be legal in C/C++. Rather, it would have to be

int const i = 0;

In reality, both work, but people end up using the first one. So,
ultimately, it adds to the confusion when dealing with more complex tyypes.
D doesn't have that problem, but since it used parens with type qualifiers,
it forces const to go on the left, making it less consistent. e.g.

const int i = 0;

or

const(int) i = 0;

So, neither C/C++ nor D is entirely consistent, but the basic rule is that
types are read outwards from the variable name, which is why you get the
weirdness with static array dimensions in D.

- Jonathan M Davis



Reply via email to