Multi-dimensional fixed arrays

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 30 14:29:04 PDT 2015


On Tuesday, 30 June 2015 at 21:02:39 UTC, DLearner wrote:
> On Tuesday, 30 June 2015 at 20:33:31 UTC, jmh530 wrote:
>> On Tuesday, 30 June 2015 at 20:17:12 UTC, Justin Whear wrote:
>>> [...]
>>
>> I think this is a good explanation.
>>
>> Looking through
>> http://dlang.org/arrays.html
>> I see that the multidimensional array indexing is not 
>> particularly focused on (could be improved?). I tend to prefer 
>> reasoning things through than relying on a rule (more likely 
>> to forget the rule). Thus, I would recommend the OP looks at 
>> the way they describe the prefix array declarations for 
>> multidimensional arrays. They have the example
>> int[4][3] b;  // array of 3 arrays of 4 ints each
>> So you can think of b as an array containing 3 arrays with 4 
>> ints each. For the OP's foo, he should think of foo as an 
>> array containing 2 arrays with 1 int each. Moreover, it's more 
>> likely that you want to index the arrays and then what's in 
>> the arrays, i.e. it's more likely that you would want to do 
>> something with the first array of foo and then the second 
>> array of foo. This notation makes it a little bit easier to do 
>> that.
>
> Out of curiosity, why can't D define a 2-dim array by something 
> like:
> int(2,1) foo;
>
> which defines two elements referred to as:
> foo(0,0) and foo(1,0)?
>
> It just seems unnatural (if not actually dangerous) to me
> to have the array index order reverse between definition and 
> use.

The notation comes from C. While I think there are many things 
that could be improved wrt arrays, I'm not sure this is one (in 
that exact way). However, improved ways to access 
multidimensional arrays is important to me. There is some work on 
improved multidimensional array support that may allow access 
that way I think.


More information about the Digitalmars-d-learn mailing list