[Swift-devel] typed keys

Mihael Hategan hategan at mcs.anl.gov
Fri Jul 22 14:19:50 CDT 2011


While integrating Yadu's patch, I came across the following issue with
appends:

When you do the auto-increment thing, the key type is a string (due to
the determinism constraints). When you declare an array with implicit
type, we decided that's an int. So, while the run-time will happily
append to an array[], that really breaks the typing when you do a
foreach v, k in array {}. I think this is why Yadu was suggesting a hash
- to get the key to be an int eventually.

There are a few solutions I can think of:
1. Have a universal type, which is what is used when saying array[].
That would allow both array[0] = value, array["key"] = value, and array
<< value. Also allow auto-increment on array[string]. This will keep
compatibility with existing scripts (i.e. allowing int keys when there
is no explicit key type in the declaration). This would require us to be
clear about when and how the universal/top type can be used.
2. Have an auto-increment key type and only allow << on those. I believe
that Ben mentioned something like this.
3. Require that arrays supporting << be array[string] and keep array[]
as array[int].

Thoughts?




More information about the Swift-devel mailing list