[Swift-devel] Semantics of multidimensional arrays in Swift

Tim Armstrong tim.g.armstrong at gmail.com
Thu Nov 17 13:58:51 CST 2011


I'm trying to understand the semantics of multidimensional arrays in Swift
so that I can implement something similar for the ExM project.

I initially assumed that 2d arrays were effectively arrays of references to
arrays, so that the semantics would be as follows:

int a[][];
int b[];

a[0] = b; // Pointer to b in first slot of array a
a[0][1] = 2; // insert into b
a[1][1] = 2; // invalid because no array inserted into a[1]


But a[1][1] = 2 actually succeeds.  As far as I can work out, what actually
happens is:

   - A[i][j] = x;
   - A[i] is uninitialised, a new array C is created, and x assigned to C[j]
      - A[i] is initialised with array C, x is assigned to C[j]
      - A[i] = B;
   - A[i] is uninitialised, A[i] is then a reference to B
      - A[i] is initialised and points to array C, all members of B are
      copied to the corresponding index in C

Is this the intended behaviour?  Am I understanding this correctly?

In the swift implementation I tested it on, this leads to some
nondeterminism:

int a[][];
int b[] = [1,2,3];

a[0][3] = 30;
a[0] = b;
a[1][0] = 123;

trace(a[0][0]);
trace(a[0][3]);
trace(a[1][0]);


Nondetermistic outcome 1
====================
SwiftScript trace: 1
SwiftScript trace: 123
Execution failed:
    Invalid path ([3]) for a.[0][]/3

Nondetermistic outcome 2
====================
SwiftScript trace: 1
SwiftScript trace: 30
SwiftScript trace: 123

- Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-devel/attachments/20111117/b89c24d1/attachment.html>


More information about the Swift-devel mailing list