Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections. In C++, you can overload "operator[]". But it can't take more than one argument. Why?<p>Because of the C "comma operator". In C, the comma operator evaluates both operands and discards the first result. It's an ancient hack used mostly with preprocessor defines.<p>In C and C++,<p><pre><code> foo(a,b)
</code></pre>
is a call with two arguments. But<p><pre><code> foo[a,b]
</code></pre>
invokes the comma operator and presents one value to "[]" You have to dig through the C++ specification to find this.<p>So I asked the question, could anyone find an example of the comma operator used in C or C++ inside square brackets in any production code. I couldn't. I got someone at IBM Almaden to search IBM's code archive and they couldn't. But there was the concern that somewhere, someone had used that misfeature for something.<p>So, no 2D array syntax for you.
Which is why the sane way to do it is to have the array-specifier come <i>before</i> the element type:<p><pre><code> [,][]int crazy;
</code></pre>
It reads "2-dimensional array of array of int" in L-to-R order.<p>Similarly, the type "pointer-to-int" should be "*p" or equivalent.<p>Unfortunately, history/tradition as well parsing problems make the "sane" solution difficult. One solution is the Pascal approach putting the type after the variable being declared, with a colon between:<p><pre><code> crazy : [,][]int;</code></pre>
Take a look at D language library Mir for N-Dimensional array and its comparison to Numpy [1].<p>Its native performance inside GLAS implementation is something to shout about and even comparable to OpenBLAS that is currently being used by Julia and Matlab [2][3].<p>[1]<a href="http://docs.algorithm.dlang.io/latest/mir_ndslice.html" rel="nofollow">http://docs.algorithm.dlang.io/latest/mir_ndslice.html</a><p>[2]<a href="http://blog.mir.dlang.io/glas/benchmark/openblas/2016/09/23/glas-gemm-benchmark.html" rel="nofollow">http://blog.mir.dlang.io/glas/benchmark/openblas/2016/09/23/...</a><p>[3]<a href="http://docs.mir.dlang.io/latest/mir_glas.html" rel="nofollow">http://docs.mir.dlang.io/latest/mir_glas.html</a>
This reasoning seems to me similar to how declaring a pointer in e.g. C works: many beginners write<p><pre><code> int* a, b;
</code></pre>
And think that this constructs two pointers-to-integer. In fact, it constructs one pointer and one plain integer. A better (equivalent) way to write it is<p><pre><code> int *a, b;
</code></pre>
You're not declaring variables of type<p><pre><code> int*
</code></pre>
You're saying that b is an int and<p><pre><code> *a
</code></pre>
Is an int, where the asterisk means "the dereferenced a"!