<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi!<br class=""><br class="">Currently, we can’t call a variadic function with an array of arguments.<br class=""><br class="">Reference:<br class="">1. <a href="http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift" class="">http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift</a><br class="">2. <a href="https://www.drivenbycode.com/the-missing-apply-function-in-swift/" class="">https://www.drivenbycode.com/the-missing-apply-function-in-swift/</a><br class=""><br class="">Consider the following use case:<br class=""><br class="">```<br class="">func average(numbers: Double…) -> Double {<br class=""> return sum(numbers) / numbers.count // Error: Cannot convert value of type ‘[Double]’ to expected argument type ‘Double'<br class="">}<br class=""><br class="">func sum(numbers: Double...) -> Double { … }<br class="">```<br class=""><br class="">Right now, there are two ways to fix it:<br class=""><br class="">1. Add another function that accept `[Double]` as input.<br class=""><br class="">```<br class="">func average(numbers: Double…) -> Double {<br class=""> return sum(numbers) / numbers.count<br class="">}<br class=""><br class="">func sum(numbers: Double...) -> Double {<br class=""> return sum(numbers)<br class="">}<br class=""><br class="">func sum(numbers: [Double]) -> Double { … }<br class="">```<br class=""><br class="">2. Implement an `apply()` function using `unsafeBitCast`.<br class=""><br class="">```<br class="">func average(numbers: Double…) -> Double {<br class=""> return sum(apply(numbers)) / numbers.count<br class="">}<br class=""><br class="">func sum(numbers: [Double]) -> Double { … }<br class=""><br class="">func apply<T, U>(fn: (T...) -> U, args: [T]) -> U {<br class=""> typealias FunctionType = [T] -> U<br class=""> return unsafeBitCast(fn, FunctionType.self)(args)<br class="">}<br class="">```<br class=""><br class="">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 class=""><br class="">Swift should allow passing an array to variadic functions, or we should somehow implement a type-safe `apply()` function in the standard library.<br class=""><br class="">Justin</body></html>