# How to sum multidimensional arrays?

AB a at a.a
Sun Mar 1 12:56:13 UTC 2020

```On Saturday, 29 February 2020 at 19:04:12 UTC, p.shkadzko wrote:
> On Friday, 28 February 2020 at 16:51:10 UTC, AB wrote:
>> On Thursday, 27 February 2020 at 14:15:26 UTC, p.shkadzko
>> wrote:
>>>[...]
>>
>> Your Example with a minimal 2D array.
>>
>> ------------
>> module test2;
>>
>> import std.random : Xorshift, unpredictableSeed, uniform;
>> import std.range : generate, take, chunks;
>> import std.array : array;
>> import std.stdio : writeln;
>>
>> struct Matrix(T)
>> {
>>  int rows;
>>  T[] data;
>>  alias data this;
>>  int cols() {return cast(int) data.length/rows;}
>>  this(int r, int c) { data=new int[r*c]; rows=r;}
>>  this(int r, int c, T[] d) {assert(r*c==data.length); data=d;
>> rows=r; }
>>
>>  auto opIndex(int r, int c) {return data[rows*c+r];}
>>
>> }
>
> Can you please explain what is the purpose of "alias data this"
> in your Matrix struct? As I remember "alias <member> this" is
> used for implicit type conversions but I don't see where "data"
> is converted.

Without "alias data this" the call

c[] = m1[] + m2[];

should be written as

c.data[] = m1.data[] + m2.data[];

With "alias data this" if some members of Matrix are not defined,
but they are available for Matrix.data, the members of
Matrix.data will be used.

It is more about names lookup rules than type conversions.

```