[swift-evolution] array splatting for variadic parameters

Tino Heth 2th at gmx.de
Fri Dec 1 11:26:42 CST 2017


There has been a solution to the same problem that’s imho much nicer, because instead of adding fundamental new syntax, it removes a piece of C-legacy:

Basically,instead of
func f(args: Int…)
you would just declare
func f(args: @variadic [Int])
or even
func f(args: [Int])
and interpret any argument that’s a comma-separated list as an array — or, and that’s imho another useful aspect, something else that can be expressed with an array literal
func f(args: Set<Int>)

So you would not only make the language surface smaller, but also add new abilities that basically come for free (they would still have to be implemented, though ;-)

> Am 30.11.2017 um 22:13 schrieb Cao, Jiannan via swift-evolution <swift-evolution at swift.org>:
> 
> What happened with this proposal? This looks easy to implement.
> 
> func sumOf(numbers: Int...) -> Int {
>       ...
>   }
> typealias Function = [Int] -> Int
> let sumOfArray = unsafeBitCast(sumOf, Function.self)
> sumOfArray([1, 2, 3])
> 
>> Hello everyone.
>> 
>> I understand that topic has already been discussed in the past, but I failed to get any information on its current state of affairs.
>> 
>> I guess the subject of this mail is explicit, but to make sure I’m clear, here's a small snippet that illustrates the problem:
>> 
>> func f(args: Int…) {
>>   // Some implementation ...
>> }
>> // Now it's impossible to call f without explicitly naming its parameters.
>> 
>> For many use-cases, this problem can be solved by overloading f so that it explicitly accepts an array.
>> 
>> func f(_ args: [Int]) {
>>   // Some implementation ...
>> }
>> 
>> func f(_ args: Int…) {
>>   f(args)
>> }
>> 
>> Some people also advocate (myself generally included) that one should prefer the signature explicitly marking args as an array, as the syntactic overhead of wrapping the arguments with “[]” when calling f is arguably bearable. However, in some other situations, this approach might not be applicable. For instance, one may simply not be able to modify the original function. Another use-case may be a function that should forward its own variadic parameters.
>> 
>> In a variety of other languages, there exists a way to do this. For instance, Python can “unpack” (or splat) a list into function arguments by prefixing it with *:
>> 
>> def f(*args):
>>   # Some implementation …
>> 
>> f(*[1, 2, 3]) # == f(1, 2, 3)
>> 
>> I was wondering if such feature could be supported by Swift, and if not, why.
>> 
>> Syntactically, I like the use of “…”, which would mirror function signatures:
>> 
>> f(…[1, 2, 3]) // == f(1, 2, 3)
>> 
>> Thanks.
>> 
>> --
>> Dimitri Racordon
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171201/f6f7710d/attachment.html>


More information about the swift-evolution mailing list