Why not remove varargs altogether from Swift, it is easy enough to put [] round a list?<br><br>On Monday, 18 April 2016, Keith Smiley via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">We've been dealing with this as well. We've chosen to go with your option 1 for<br>
most of our cases, sometimes dropping varargs all together and just using the<br>
array signature.<br>
<br>
It would be great if you could have a safe apply function for this.<br>
<br>
--<br>
Keith Smiley<br>
<br>
On 04/17, Justin Jia via swift-evolution wrote:<br>
> Hi!<br>
><br>
> Currently, we can’t call a variadic function with an array of arguments.<br>
><br>
> Reference:<br>
> 1. <a href="http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift" target="_blank">http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift</a> <<a href="http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift" target="_blank">http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift</a>><br>
> 2. <a href="https://www.drivenbycode.com/the-missing-apply-function-in-swift/" target="_blank">https://www.drivenbycode.com/the-missing-apply-function-in-swift/</a> <<a href="https://www.drivenbycode.com/the-missing-apply-function-in-swift/" target="_blank">https://www.drivenbycode.com/the-missing-apply-function-in-swift/</a>><br>
><br>
> Consider the following use case:<br>
><br>
> ```<br>
> func average(numbers: Double…) -> Double {<br>
> return sum(numbers) / numbers.count // Error: Cannot convert value of type ‘[Double]’ to expected argument type ‘Double'<br>
> }<br>
><br>
> func sum(numbers: Double...) -> Double { … }<br>
> ```<br>
><br>
> Right now, there are two ways to fix it:<br>
><br>
> 1. Add another function that accept `[Double]` as input.<br>
><br>
> ```<br>
> func average(numbers: Double…) -> Double {<br>
> return sum(numbers) / numbers.count<br>
> }<br>
><br>
> func sum(numbers: Double...) -> Double {<br>
> return sum(numbers)<br>
> }<br>
><br>
> func sum(numbers: [Double]) -> Double { … }<br>
> ```<br>
><br>
> 2. Implement an `apply()` function using `unsafeBitCast`.<br>
><br>
> ```<br>
> func average(numbers: Double…) -> Double {<br>
> return sum(apply(numbers)) / numbers.count<br>
> }<br>
><br>
> func sum(numbers: [Double]) -> Double { … }<br>
><br>
> func apply<T, U>(fn: (T...) -> U, args: [T]) -> U {<br>
> typealias FunctionType = [T] -> U<br>
> return unsafeBitCast(fn, FunctionType.self)(args)<br>
> }<br>
> ```<br>
><br>
> However, both solutions are not very elegant. The first solution requires the library author to implement both functions, and the second solution breaks the guarantees of Swift’s type system.<br>
><br>
> Swift should allow passing an array to variadic functions, or we should somehow implement a type-safe `apply()` function in the standard library.<br>
><br>
> Justin<br>
<br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
</blockquote><br><br>-- <br>-- Howard.<br>