[swift-evolution] Splat

Chris Lattner clattner at apple.com
Wed Feb 10 22:09:46 CST 2016

> On Feb 10, 2016, at 2:50 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>> There was a discussion about using `…` as an _array splat_ operator, i.e:
>>   func foo(xs: Int…)
>>   let xs = [1, 2, 3]
>>   foo(xs…)
>> That would be symmetric with “…” defining a variable length argument, and also the same approach as Ruby, but instead of `*xs` which looks like memory dereferencing, `xs…`, which IMHO works better as a symbol for this.
>> Perhaps it would be possible to overload this operator to perform _tuple splat_ as well. (I haven’t put it through tough thorough thought, though.)
> When I was looking up how different languages handle splatting, I noticed that `...` is PHP's splat operator. But keep in mind that, even though some of them can be typed pretty tightly if you want, Ruby, Perl 6, and PHP all have much looser *attitudes* towards typing than Swift; their splat operators are used to unpack heterogenous arrays into argument lists, with overflowing elements either being dropped or going into a specially-marked array parameter.
> Swift is a very different language; this feature is meant for strongly-typed tuples, and is basically orthogonal to unpacking an array into a variadic parameter. I think that giving the "unpack tuple into multiple parameters" and "unpack array into a single variadic parameter" operators the same name would be confusing, and given that variadic parameters are declared with `...`, that operator should be reserved for variadic unpacking.

Using … for this makes sense.  Forwarding a varargs array using "foo(varargarray…)” would be the seemingly most obvious way to unpack “varargsarray” as the varargs list.  Generalizing that to “foo(tuple…)” seems analogous and unambiguous.

> (By the way, are we going to drop variadic tuples? With the demise of parameters-as-tuples, I'm not sure there's much of a point in them anymore.)

I’m pretty sure that variadic tuples are already dead, is there some case still limping along?


More information about the swift-evolution mailing list