Fixed size multidimensional array at runtime

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 30 12:06:16 PDT 2012


On Saturday, June 30, 2012 21:01:02 Vidar Wahlberg wrote:
> On Saturday, 30 June 2012 at 18:32:09 UTC, Jonathan M Davis wrote:
> > In D, static arrays are always fixed in size, and that size
> > must be known at
> > compile time, whereas dynamic arrays are never fixed in size
> > (unless they're
> > immutable), and the size doesn't need to be known at compile
> > time. There is no
> > way to have a static array whose size isn't known at compile
> > time, which is
> > what you'd need for what you're trying to do.
> 
> Thanks for the answer. It makes sense, yet when used to
> multidimensional arrays as implemented in Java it takes some time
> to wrap your head around this.
> 
> > I believe that the only way to do it would be to create a
> > struct which wrapped
> > a single dimensional, dynamic array, and then overloaded
> > opIndex appropriately
> 
> This is a very good suggestion, I hadn't thought of this
> possibility, this way I can get my beloved "matrix[x][y];"
> instead of something like "matrix.get(x, y);".

It might have to be matrix[x, y] though, since while you can make opIndex take 
as many arguments as you want, I don't believe that it will let you split them 
out into multiple indexing operations like in matrix[x][y]. If you really want 
that, you'd have to create a helper type which opIndex returned and have that 
helper type overload opIndex for the second index. But if efficiency is your 
main concern, that might be a problem, since I expect that that adds some 
overhead (though probably not a lot).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list