[Swift-devel] Associative arrays: foreach index issue.

Mihael Hategan hategan at mcs.anl.gov
Fri Jun 24 13:51:53 CDT 2011


Ok, so slow down a bit.

On Sat, 2011-06-25 at 00:09 +0530, Yadu Nand wrote:
> Hi,
> 
> After discussing with Justin I used java.util.UUIDs as indices for
> cases in which we need something close to appending the value
> associated with a particular key.

We've established that any random id is as bad as a sequential id. So
use the thread index instead if you really want this. But I think we
also established that we don't really need implicit indices.

> 
> Now,  array["key"]["!"] = 1 ; array["key"]["!"] = 2 ;
> works alright, except for the part in which we use foreach to retrieve
> the values as the index is actually a UUID casted to string.
> 
> Justin asked me to propose a way to fix this, including a new
> associative array declaration syntax.
> 
> 1. We may try using a different declaration style to denote non-int
>      subscripts.
>      int array [$] [$]   ( where $ represents strings )

That's random an ugly. We should use the exact type for declarations:

int array[string].

> 2. Or simply modify the foreach syntax to accommodate string
>      indices. Leaving the following style
>      foreach value, string:index in array["key"] {
>         // do work
>      }

And what happens if there is a mismatch between the foreach index type
and the array type?

Point being that we can use type inference to figure out the type of the
index since we know the type of the array key.

> 
> I can't think of anything simpler and which makes more meaning.
> Please help!
> Of the 2 I mentioned above, I think the 2nd method is the easiest
> to implement.
> 





More information about the Swift-devel mailing list