[Swift-devel] Associative array in Swift [GSoC]

Ben Clifford benc at hawaga.org.uk
Sat Jun 18 09:48:46 CDT 2011


> But then we can already have arrays of arrays, so I'm not quite
> sure what the issue is here.

There might be some use in list-like syntax, rather than array-like 
syntax. Or there might not be.

By that I mean that when you assign to an array, you have to have some 
value to index it. If you're doing some arbitrary nested foreach loops to 
populate an output array you have to construct an index out of some thing 
from each enclosing loop. eg:

  foreach i in a {
    foreach j in b {
      output[i+j*100000] = f(i,j)
      // or output[i][j] = f(i,j)
    }
  }

If you happen to not care about that (i.e. you care about the collection 
of values output by f, but not about their indices) then that [i][j] 
structure or [i+j*100000] structure is a bit artificial.

When you (in Haskell) write:

   a = a ++ (f i j)

then you add an element to the collection a without having to contrive an 
index - in as much as lists are indexed by their position, the new element 
gets "the next one". "the next one" makes sense in sequential code.

In a Swift context, I could imagine a syntax:

  a[!] = f(i,j)

where ! means "some appropriate unique index" (I think the restart code 
did/does do that foreach loops to label variables in nested scope in a way 
that could be used here).

So then Yadu's: a[0] = 100; a[0] = 200; example might turn into:

  int a[][]; a[0][!] = 100; a[0][!] = 200;

That parallelises still but doesn't require you to explicitly think about 
the index.

-- 



More information about the Swift-devel mailing list