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

Vladimir.S svabox at gmail.com
Wed Apr 20 01:36:38 CDT 2016

At this moment I think we should keep "variadic" feature just because it is 
very handy and useful practically, in our day-by-day coding. Without it 
we'll have to write a number of overloaded functions or use [] all the time.
I.e. I believe nobody wants to write
, and for this we'll need declarations

Plus, array of values is not the same as a list of parameters at the end. 
Using of array instead of parameters probably confuses more than variadic 
parameters for the same function name.

So, the cons of having variadic functions are much less than the pros.

But +1 to be able to use array as parameters by using some #splat 
compiler-time func, i.e. print(#splat(arr)) - explicit and clear

On 19.04.2016 22:25, Justin Jia wrote:
> Pros of removing variadic functions:
> 1. Slightly easier to understand.
> 2. Simpler language design.
> Cons of removing variadic functions:
> 1. More verbose syntax.
> 2. Break old code.
> I understand your reasons. But,
> 1. People have been using NSLog and StringWithFormat for many years and
> most people don't have any difficulty understanding them. So the benefit of
> removing this feature is very subtle.
> 2. The solution I proposed to fix is very straightforward. I don't think it
> will too much cost. (Maybe we need someone who is familiar with compiler to
> compare the cost).
> Also,
> 1. Sometimes we need simpler syntax. Like this project:
> https://github.com/JustinJiaDev/SwiftShell
> I prefer to write ls(.all, .longFormat) instead of ls([.all, .longFormat]).
> That's really important if we want to use it as a shell.
> 2. It not only breaks the language itself, it will  also break cocoa APIs.
> That is bad.
> Justin
> On Apr 19, 2016, at 10:54 AM, Haravikk <swift-evolution at haravikk.me
> <mailto:swift-evolution at haravikk.me>> wrote:
>>> On 19 Apr 2016, at 17:51, Vladimir.S <svabox at gmail.com
>>> <mailto:svabox at gmail.com>> wrote:
>>> I.e. the question is *if we want/need to be able to pass array to
>>> existed variadic function*. Not about removing the variadic
>>> feature("removing" is offtop for this poposal, as I understand)
>> 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:
>> *Pros*:
>>   * Slightly cleaner syntax at call-site.
>>   * Possibly optimisations unavailable to Array passing?
>> *Cons*:
>>   * 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, as
>> the first time you’re presented with one it isn’t necessarily clear what
>> it does unless you’ve encountered them before. Like I say it can be
>> ambiguous at the call-site in particular, as it doesn’t show that an
>> array of [Foo] is being passed as opposed to N instances of Foo (however
>> many are in the call).
>> While I’ve used them in the past, I’ve never really felt that they
>> simplify anything enough to justify them, as it’s just two square
>> brackets extra to pass an array; this is extra noise sure, but clarifies
>> what is actually happening.
>> So if variadic functions don’t have any other advantages, then it’s
>> really just a very minor piece of syntactic sugar that can lead to more
>> confusion, less explicit types in function calls, an extra piece of
>> syntax to parse and handle and possibly other features required to
>> support it better (like the one being inquired about). The only other
>> argument I can think of for them is that many other languages have them,
>> but that’s not important to me vs cutting out cruft.
>> Short version; removing variadic functions would solve the problem of
>> defining overloads for both variadic and array types by only ever
>> requiring the latter.
>> 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 ;)

More information about the swift-evolution mailing list