[swift-evolution] [Idea] Passing an Array to Variadic Functions

Haravikk swift-evolution at haravikk.me
Wed Apr 20 07:10:00 CDT 2016

> On 20 Apr 2016, at 11:44, Jeremy Pereira <jeremy.j.pereira at googlemail.com> wrote:
>> Pros:
>> 	• Slightly cleaner syntax at call-site.
>> 	• Possibly optimisations unavailable to Array passing?
> • Makes calling C and Objective-C functions with variadic parameters less confusing
> • Will not break existing Swift code that uses variadic parameters

In both cases you just have to add square brackets to make it an array instead, which doesn’t seem more confusing to me since that’s actually what you’re doing anyway.

> • Makes generalising some functions cleaner e.g. zip(a, b) could be generalised to any number of parameters as already mentioned, but if you instead used an array, we would end up with a massive thread about whether the original two parameter zip should be removed

Only if the two-parameter form isn’t implemented any differently; however, being that it’s much simpler case it should be, so there’s a good reason to keep it. Plus with no variadic form masquerading as the same thing there’d be no ambiguity, i.e- it’s clear that they may be implemented differently rather than variadic masquerading as fixed. This comes back to it being clearer at the call-site that an array is being passed, which currently isn’t the case.

> My quick an uscientific survey of questions and answers relating to variadic parameters on StackOverflow yields no instances of people asking what does the`…` in `func foo(a: Int…)` do which suggests that the learning curve is actually fairly trivial. However their is a megaton of questions asking things like “why can’t I pass an array" and "how do I forward variadic arguments to another variadic function”.

The ellipsis syntax is straightforward enough, but I think the problem lies more with variadics that overload similar function signatures (like the zip() method) where it becomes less clear what you’re calling, and what’s happening in that call (even if the actual method is pretty much the same). Passing arguments on also couldn’t be simpler in the array form, as you simply pass the array into another function that can take one.

More information about the swift-evolution mailing list