Dynamic multidimensional arrays

Jonathan M Davis jmdavisProg at gmx.com
Tue Jul 5 11:44:47 PDT 2011


On 2011-07-05 11:29, Trass3r wrote:
> > If you want it to by dynamic all the way, you need to put
> > the dimensions in the parens like above. Personally, I _never_ put them
> > in the brackets, even when the dynamic array has just one dimension.
> > It's just
> > simpler to always put them in the parens and not worry about it.
> 
> Maybe we should force the parens approach even for one dimension.
> At least dmd should give better diagnostics. It's damn hard to even find
> that syntax in the docs.

The problem is that there's nothing wrong with putting the dimensions in the 
brackets. It makes perfect sense _if you want dynamic arrays of static 
arrays_. However, that's not normally what people want. _Everyone_ who 
attempts to create a multidimensional dynamic array runs into this problem.

Putting the dimension in the brackets for a single dimension dynamic array 
matches exactly what's going on when you put the dimensions in the brackets 
with multi-dimension arrays. So, disallowing it wouldn't really make sense.

The issue is that it having multi-dimensional arrays where elements are static 
arrays makes perfect sense under certain circumstances, and it uses pretty 
much exactly the syntax that you'd expect it to if you were looking for that, 
but that that syntax is exactly what people try to use when using multi-
dimensional arrays which are purely dynamic. So, this screws over everyone, 
but changing it risks removing a valid feature.

So, I really don't know how it should be handled. I fear that it's like how

int[5][4] a;
int a[4][5];

produce the same array. It matches perfectly how the language works in general 
but is exactly the opposite of what programmers expect, since they don't think 
like a compiler. Changing it makes the language inconsistent, but keeping it 
as-is causes problems for everyone using the language. It sucks pretty much no 
matter what you do. Maybe someone can come up with a way to clean things up, 
but as it stands, static arrays tend to screw over programmers until they 
understand their quirks (generally after getting bitten by errors like the OP 
ran into).

The one thing that _is_ clear, however, is that if the online docs are not 
clear about the paren syntax, then they should be updated. And they should 
probably explain the issue of dynamic arrays of static arrays more clearly so 
that those who read the docs will at least have some clue what is going on and 
avoid this issue.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list