I'm trying to understand the semantics of multidimensional arrays in Swift so that I can implement something similar for the ExM project. <br><br>I initially assumed that 2d arrays were effectively arrays of references to arrays, so that the semantics would be as follows:<br>
<br>int a[][];<br>int b[];<br><br>a[0] = b; // Pointer to b in first slot of array a<br>a[0][1] = 2; // insert into b<br>a[1][1] = 2; // invalid because no array inserted into a[1]<br><br><br>But a[1][1] = 2 actually succeeds. As far as I can work out, what actually happens is:<br>
<ul><li>A[i][j] = x;<br></li><ul><li>A[i] is uninitialised, a new array C is created, and x assigned to C[j]<br></li><li>A[i] is initialised with array C, x is assigned to C[j]<br></li></ul><li>A[i] = B;<br></li><ul><li>A[i] is uninitialised, A[i] is then a reference to B</li>
<li>A[i] is initialised and points to array C, all members of B are copied to the corresponding index in C</li></ul></ul>Is this the intended behaviour? Am I understanding this correctly?<br><br>In the swift implementation I tested it on, this leads to some nondeterminism:<br>
<br>int a[][];<br>int b[] = [1,2,3];<br><br>a[0][3] = 30;<br>a[0] = b;<br>a[1][0] = 123;<br><br>trace(a[0][0]);<br>trace(a[0][3]);<br>trace(a[1][0]);<br><br><br>Nondetermistic outcome 1<br>
====================<br>SwiftScript trace: 1<br>SwiftScript trace: 123<br>Execution failed:<br> Invalid path ([3]) for a.[0][]/3<br><br>Nondetermistic outcome 2<br>====================<br>SwiftScript trace: 1<br>SwiftScript trace: 30<br>
SwiftScript trace: 123<br><br>- Tim<br>