<div dir="ltr"><div>Hi All,<br><br></div><div>I've been working on adding appendable data structures to Swift/T.  I thought I would email to clarify a couple of points about the [auto] arrays in Swift/K and also to get feedback on the Swift/T implementation.  Ideally we'd do things the same way in both but I'm not confident that the Swift/K semantics will work well for us.<br>
<br></div><div>First, clarifying questions, since I'm not 100% clear on what happens when you "use auto key values from one array to access another", e.g. the example:<br><span style="font-family:courier new,monospace"><br>
======================<br>int[auto] a;<br>int[auto] b;<br><br>append(a, 1);<br>append(a, 2);<br><br>foreach v, k in a {<br>  b[k] = a[k] * 2;<br>}<br>=====================</span><br>
</div><div><br></div><div>There are a few corner cases I don't quite understand in the above example.<br>Are there any guarantees about how the keys are assigned, e.g. in order from 0?  What is the type of k? Can you treat it as an integer e.g. to trace?<br>
<br></div><div>Also, is it possible to double-assign an index by doing that?  E.g. if I append(b, 1); before the loop, is it possible for that to conflict with one of the b[k] = ... assignments?<br></div><div><br></div><div>
Anyway, in Swift/T I've been playing around with an unordered set data structure: a "bag": with some similar functionality.  There were a couple of reasons I didn't just clone the Swift/K version:<br><ul>
<li>We can't efficiently sequentially assign indices if the data structure is split across multiple nodes (currently not the case, but possible in future).</li><li>The append(A, x) operation that mutates it's first argument is a little awkward semantically/implementation-wise, since it looks like a function call but can't be handled as one: the first argument is an LValue whereas a function argument is an RValue.  The logic for handling L/RValues in Swift/T is quite different in the grammar/frontend/semantic analyser.  I can easily enough make "append" a reserved word and special-case it in the grammar to work around this, but it feels a bit "wrong" and inconsistent to have something that looks like a function that's doesn't obey the semantics of a function.  I remember Swift/K had a += operator at one point - what was the argument for moving away from it?</li>
</ul><p>Currently this is what it looks like (with the Swift/K equivalent for comparison).  I'd be curious to hear people's thoughts are on how to improve it.</p></div><div>Swift/T (draft):<br><span style="font-family:courier new,monospace">
===================<br> bag<int> X;<br><br></span></div><span style="font-family:courier new,monospace">  X += 1;<br></span><div><span style="font-family:courier new,monospace">  X += 1;<br></span></div><div><span style="font-family:courier new,monospace">  X += 2;<br>
</span></div><div><span style="font-family:courier new,monospace">  foreach x in X {<br>    trace(x);<br>  }<br>===================<br></span><br>Swift/K equivalent:<br><span style="font-family:courier new,monospace">===================<br>
  int X[auto];<br><br>  append(X, 1);<br></span><div><span style="font-family:courier new,monospace">  append(X, 1);<br></span></div><div><span style="font-family:courier new,monospace">  append(X, 2);<br></span></div><span style="font-family:courier new,monospace">  foreach x in X {<br>
    trace(x);<br>  }<br>===================</span><br><br></div><div>- Tim<br></div></div>