[Swift-devel] New syntax for appending to arrays
Ben Clifford
benc at hawaga.org.uk
Tue Jun 28 13:34:52 CDT 2011
On Jun 28, 2011, at 7:37 PM, Yadu Nand wrote:
>
> I'd like to point out one issue that came up between a discussion with
> Justin today. I am using the earlier idea of the append being an assign
> with the array subscript being the thread prefix.
> Meaning array["key"] += 10 is [1]converted to array["key"]["!"] = 10 by
> the parser.
> There is still the possibility that the user may inadvertently use the string
> "!" as subscript and create weird situations. So instead of "!" we could use
> a string say "_SWIFT_AUTO_INCREMENT" . This being something used
> internally.
You could approach this at the type level too, if you're putting types for indices.
A user can only specify ["!"] as an index if the array takes a string in that position.
You could have a different type (for the sake of argument called 'auto' (cue another naming discussion)) for automatically assigned indices. (which in the implementation under question are thread IDs - but that's an accident of implementation).
So if I am going to say array[2] += 10; then array must have been declared with type:
int array[int][auto];
and if I'm going to say
array[2]["!"] = 10; (meaning, use key "!", not use an automatically assigned variable)
then array must be declared as:
int array[int][string].
In that approach, it is a type checking error to either use += on a non-auto array, or specify an explicit string or int index on an auto array.
You could then have variables with type auto, which you could extra auto values into through foreach, but not other wise being able to specify auto values.
--
More information about the Swift-devel
mailing list