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

Ben Clifford benc at hawaga.org.uk
Fri Jun 24 14:30:31 CDT 2011


On Jun 24, 2011, at 8:51 PM, Mihael Hategan wrote:

> 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.

agree.

> But I think we
> also established that we don't really need implicit indices.
> 

disagree. I think they can make coding simpler by allowing Swift to figure out stuff for you rather than you having to express boilerplate indices that you can get wrong.

I think the ["!"] syntax is awful - I suggested it only for the purposes of my original message for the sake of having *something*.

>> 
>> 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].

agree.

(Part of my justification: conceptually (even if it is never implemented) any data type that has a sensible equality relation can be used as an index. Other examples are booleans, and structs composed of other types with equality.)

> 
>> 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.

agree. originally foreach had a type spec for 'value' too, which was replaced by inference - it could never contain any useful information.




More information about the Swift-devel mailing list