Rectangular Arrays

Bill Baxter dnewsgroup at
Thu Feb 22 23:24:01 PST 2007

Another Roadside Attraction wrote:
> Thanks, Bill!!
> I hadn't seen the array initialization syntax with the parenthetized arguments. It's kind of odd-looking. Do know know anything about its etymology? Since arrays already have bracketized declaration syntax, the parenthetized syntax seems like an unnecessary kludge.

Yeh, I have to look it up every time I need it.

But if you think of int[][][] as the complete type, it kind of makes 
sense.  It's just Type(arguments) where Type is int[][][] and arguments 
are (5,10,20).  So it's actually very consistent with standard 
constructor syntax.  In fact I'd guess you can probably do:

alias int[][][] Type;
Type x = new Type(5,10,20);

and it will probably work.

...And now that I've realized that, I probably won't forget that syntax 
ever again.  :-)

> Anyhow, the parenthetized syntax doesn't seem to be anywhere on the digital mars website or in any of the dsource tutorials.

Yeh, it should be added to arrays, or at least the wiki comments page. 
Could you add it if you have a sec?

> Unfortunately, your multi-array library won't work for me, since it relies on templates. I won't know the size of the grid until runtime, so templates are a no-go.

Nope that's not an issue, at least with my multi-array lib.  You only 
need to know the _type_ of the elements at compile time.
    int[] sz = [5,20,30];
    auto array1 = new ndarray!(int)(sz);
    auto array2 = new ndarray!(int)(sz[0],sz[1],sz[2]);

But really you don't need it if you're just trying to make a grid of 
numbers that's accessibly by index.

> But the ragged arrays should be fine for now.

Yep, I suspect so as well.

> Thanks again,

No prob.

> --ARA
> Bill Baxter Wrote:
>> D doesn't have rectangular arrays at the moment.  But you can init a 
>> multi-dim ragged array.
>> int[][][] bar = new int[][][](5,20,30);
>> or
>> auto bar = new int[][][](5,20,30);
>> I have some nd rectangular array at 
>> but it sounds like for your purposes the ragged array will be sufficient.
>> --bb

More information about the Digitalmars-d-learn mailing list