[Swift-devel] Appendable data structures in Swift/K and Swift/T

Tim Armstrong tim.g.armstrong at gmail.com
Wed Sep 4 12:23:55 CDT 2013


Hi All,

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.

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:

======================
int[auto] a;
int[auto] b;

append(a, 1);
append(a, 2);

foreach v, k in a {
  b[k] = a[k] * 2;
}
=====================

There are a few corner cases I don't quite understand in the above example.
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?

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?

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:

   - We can't efficiently sequentially assign indices if the data structure
   is split across multiple nodes (currently not the case, but possible in
   future).
   - 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?

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.
Swift/T (draft):
===================
 bag<int> X;

  X += 1;
  X += 1;
  X += 2;
  foreach x in X {
    trace(x);
  }
===================

Swift/K equivalent:
===================
  int X[auto];

  append(X, 1);
  append(X, 1);
  append(X, 2);
  foreach x in X {
    trace(x);
  }
===================

- Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/swift-devel/attachments/20130904/0a88dd60/attachment.html>


More information about the Swift-devel mailing list