What is the difference between a[x][y] and a[x,y]?

Robert M. Münch robert.muench at saphirion.com
Tue Jan 7 18:17:44 UTC 2020


On 2020-01-07 17:42:48 +0000, Adam D. Ruppe said:

> So [x][y] indexes an array of arrays.

Yes, that's what I understand. And both can be dynamic, so that I can 
have a "flattering" layout where not all arrays have the same length.

>  [x,y] indexes a single array that has two dimensions.

Does this fix the dimension sizes? So I can't add a "row" or "column" 
at runtime?

What are the difference use-cases for these two? For example, I'm doing 
a grid widget but want to add/remove rows/columns. Can this only be 
done with a array-of-array? Is the memory continuous in the [x,y] case, 
whereas in the [x][y] this is not necessarily the case?

> This can be kinda confusing because we often think of
> 
> int[4][4]
> 
> as being a 2d array, but the D language actually technically sees that 
> as an array of arrays. So it is indexed with [x][y].

Yes, it's confusing as I don't understand the [x,y] case :-)

> There is no built in multi-dimensional array, it is only syntax 
> provided for library types to implement with the opIndexAssign 
> overloads.

I don't get that sentence. Above you write "a single array that has two 
dimensions" which IMO is exactly a multi-dimensional array.

> So the operator there like += or -= or *= etc become opAssign, whereas 
> plain = is just plain Assign.

Got it. Thanks.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20200107/8886f8ba/attachment.htm>


More information about the Digitalmars-d-learn mailing list