[swift-evolution] [Discussion] What is the future of tuples in Swift?

Stephen Canon scanon at apple.com
Fri Mar 3 09:52:55 CST 2017


> On Mar 3, 2017, at 7:57 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The question here is, how is a tuple different from a mathematical vector? I wasn’t referring to vector concepts from other languages, but used the keyword vector to indicate a shortcut that provides a fixed or an arbitrary length such as …T would do in variadic generics context.
> 
> A shortcut for (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) is probably not that bad to have. In my sketch it would be (vector(16) Int), but it still would be a tuple. Structs aren’t arrays, but we created a value type that represents dynamic arrays (called vectors in different languages) and called it Array. Furthermore the dynamic array is not a simple primitive type and contains a whole bunch of other things.
> 
> That said, I don’t see why tuples should not benefit from all of that, if features like parametrized extensions make it into Swift one day.
> 
> extension<…T> (T…) : MutableCollection { … }
> 
> // or from my sketch  
> extension<vector generic T> (vector T) : MutableCollection { … }
> Being able to tell the size of such a vector is more beneficial than only having the … pre- and postfix for arbitrary length.
> 
> Currently variadics return Array<T>, which is *kinda* fine, but is not fully correct, because the returned sequence cannot be empty.
> 

There are a number of ways in which vectors are different from tuples:

Relatively obvious:
- vectors are homogenous, tuples are not (necessarily). One could say that vectors are just homogenous tuples, but:
- vectors should (probably) have arithmetic operators. Tuples, even homogenous tuples, should (probably) not, because they may represent types for which arithmetic doesn’t make sense, or for which arithmetic might need to do different things per-lane.

More subtle:
- vectors and tuples want to have different machine-level calling conventions. If arithmetic is done on tuples at all, it is likely to do different things per-lane, so tuples are best passed with each lane in a different register. Vectors generally do arithmetic uniformly across lanes, so the most efficient calling convention is usually to put the vector contiguously in a vector register (if the targeted architecture has sufficiently large registers).

Vectors, Tuples, and Structs are all closely related concepts with a lot of overlap, but also some key differences. If anything, IMO a Tuple is closer to an anonymous ad-hoc Struct than it is to a Vector.

– Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170303/00e22428/attachment.html>


More information about the swift-evolution mailing list