Differing semantics between multidimensional fixed-length array and slice initialization

Chris Cain clcain at uncg.edu
Tue Apr 2 19:28:53 PDT 2013


On Wednesday, 3 April 2013 at 01:30:19 UTC, ixid wrote:
> How can you call the new syntax better? You assign arrays' 
> lengths in the opposite to that that you access them. It's a 
> horrible design mistake.

Unfortunately, I have to agree that the new syntax is better. 
int[5][6] is confusing as a type because it's read from right to 
left, but it makes much more sense than int[6][5].

Consider how you would read the "old style"

It's a 6-long array of 5-long arrays of int.

So, the new style is simply read from right to left. But using 
the other way ... it's read from the center to right, and finally 
to the front.

How about throwing AAs into the mix?

int[string][5][6]

What would that mean? The old style would be incredibly 
confusing. It'd be an associative array mapping strings to 5-long 
arrays of 6-long arrays of ints. Right?

Plus consider this:

alias Dict = int[string]

Dict[5][6] vs int[string][5][6]

Using the new way, these two are the same. Using the "old style" 
... they're not.


Really though, bearophile has it right. C (and, D, transitively) 
made a huge mistake in type declarations. They ought to be read 
from left to right (like Pascal). C is crazy in that they've 
designed it so you read types in a spiral pattern.

What's a int*? It's a pointer to an int. Why isn't it a *int, 
then? How about [5]int? It's a 5-long array of int. [5][6]int ... 
It's a 5-long array of 6-long arrays of integers. And so on.


More information about the Digitalmars-d-learn mailing list