[swift-evolution] [Idea] Passing an Array to Variadic Functions
jeremy.j.pereira at googlemail.com
Wed Apr 20 05:44:35 CDT 2016
> On 19 Apr 2016, at 18:54, Haravikk <swift-evolution at haravikk.me> wrote:
I think your lists are incomplete.
> I’d say it’s on-topic, as removing variadic functions would eliminate the problem entirely, so if it is considered a better alternative then there would be no need to have an additional means of calling them with an array of arguments.
> Personally the way I see it comes down to variadic functions is this:
> • 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
• 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
> • Doesn’t do anything that passing an array directly can’t.
> • Passing an array is actually slightly more flexible (can dynamically pass more or less arguments as required at the call site).
> • Less explicit type at call site; gives the appearance of passing instance(s) of Foo, rather than one instance of [Foo], can lead to ambiguity with overloaded functions.
> • Extra syntax to support (enabling array passing would be more code required to support this feature)
> I’d also argue that variadic functions increase the learning curve,
You are coming at this from the wrong direction. You should be arguing that _removing_ variadic functions would _reduce_ the learning curve. This is true, but only something to worry about if the learning curve is currently too steep and needs reducing.
My quick and unscientific 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".
> P.S- I’d also like to note that where possible people should be accepting Sequences or Collections in their methods anyway rather than arrays specifically, as it’s more flexible that way ;)
Now there’s an example of a steep learning curve in Swift 2.
More information about the swift-evolution