[swift-evolution] Splat

Brent Royal-Gordon brent at architechies.com
Wed Feb 10 16:50:33 CST 2016

> 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.

(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.)

Brent Royal-Gordon

More information about the swift-evolution mailing list