Differing semantics between multidimensional fixed-length array and slice initialization

ixid nuaccount at gmail.com
Wed Apr 3 08:44:51 PDT 2013


On Wednesday, 3 April 2013 at 02:28:55 UTC, Chris Cain wrote:
> 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.

I wasn't arguing for the old style, the new declarations should 
have read from left to right so [6][5][string]int and then you 
access the elements in the same order that you assigned them, 
that seems far less confusing and leaves less room for sleepy 
mistakes. It seems like a pointless mental overhead so have to 
keep swapping the order of your arguments.


More information about the Digitalmars-d-learn mailing list