[Swift-devel] New syntax for appending to arrays

Mihael Hategan hategan at mcs.anl.gov
Tue Jun 28 13:56:55 CDT 2011


I very much agree with this, except I wouldn't say that one could
approach this from a type system perspective, but it is necessary to do
so. Using auto-indices on an int array would break things if the
auto-indices are string (which they are). In other words, the following
code would pass type checking but is essentially incorrect:
int a[];
a += 1; a += 2;

foreach v, k in a {
  trace(k + 1);
}

On Tue, 2011-06-28 at 20:34 +0200, Ben Clifford wrote:
> 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