Array construction - why backwards?

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Apr 15 05:25:02 PDT 2008


"Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
news:fu1g8u$1af6$1 at digitalmars.com...
>
> It makes declarations more consistent that way.
>
> T[5]  is a type that means array of 5 T's.
>
> If T is an int that means you have an int[5].
>
> Now what if T is an int[3]?
>
> Isn't it logical that to make an array of 5 of those you would do:
> T[5] ==> (int[3])[5] ==> int[3][5]

Exactly.

Another reason is when you have mixed "extended" types.  This is confusing:

int** foo[3][4];

There are decorators on both sides?  Huh?  This is far more readable:

int**[4][3] foo;

Since, just like every other declaration, you read right to left: a 
3-element array of 4-element arrays of pointers to pointers to ints.

Bring function pointers into the mix and.. well you get the idea.  Since 
every "composite" type follows the same right-to-left order, it makes things 
very easy to grok without having to use some kind of utility to decode types 
(like there are for C). 




More information about the Digitalmars-d-learn mailing list