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

Dennis Weissmann dennis at dennisweissmann.me
Mon Apr 18 02:35:51 CDT 2016


> Why not remove varargs altogether from Swift, it is easy enough to put [] round a list?

+1, that was my thought too. I can’t think of a use case where you can’t use an array instead of varargs (this assumes all vararg parameters are converted to array parameters).

- Dennis

> On Apr 18, 2016, at 12:48 AM, Howard Lovatt via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Why not remove varargs altogether from Swift, it is easy enough to put [] round a list?
> 
> On Monday, 18 April 2016, Keith Smiley via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> We've been dealing with this as well. We've chosen to go with your option 1 for
> most of our cases, sometimes dropping varargs all together and just using the
> array signature.
> 
> It would be great if you could have a safe apply function for this.
> 
> --
> Keith Smiley
> 
> On 04/17, Justin Jia via swift-evolution wrote:
> > Hi!
> >
> > Currently, we can’t call a variadic function with an array of arguments.
> >
> > Reference:
> > 1. http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift <http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift> <http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift <http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift>>
> > 2. https://www.drivenbycode.com/the-missing-apply-function-in-swift/ <https://www.drivenbycode.com/the-missing-apply-function-in-swift/> <https://www.drivenbycode.com/the-missing-apply-function-in-swift/ <https://www.drivenbycode.com/the-missing-apply-function-in-swift/>>
> >
> > Consider the following use case:
> >
> > ```
> > func average(numbers: Double…) -> Double {
> >    return sum(numbers) / numbers.count // Error: Cannot convert value of type ‘[Double]’ to expected argument type ‘Double'
> > }
> >
> > func sum(numbers: Double...) -> Double { … }
> > ```
> >
> > Right now, there are two ways to fix it:
> >
> > 1. Add another function that accept `[Double]` as input.
> >
> > ```
> > func average(numbers: Double…) -> Double {
> >    return sum(numbers) / numbers.count
> > }
> >
> > func sum(numbers: Double...) -> Double {
> >    return sum(numbers)
> > }
> >
> > func sum(numbers: [Double]) -> Double { … }
> > ```
> >
> > 2. Implement an `apply()` function using `unsafeBitCast`.
> >
> > ```
> > func average(numbers: Double…) -> Double {
> >    return sum(apply(numbers)) / numbers.count
> > }
> >
> > func sum(numbers: [Double]) -> Double { … }
> >
> > func apply<T, U>(fn: (T...) -> U, args: [T]) -> U {
> >    typealias FunctionType = [T] -> U
> >    return unsafeBitCast(fn, FunctionType.self)(args)
> > }
> > ```
> >
> > 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.
> >
> > Swift should allow passing an array to variadic functions, or we should somehow implement a type-safe `apply()` function in the standard library.
> >
> > Justin
> 
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org <>
> > https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> 
> -- 
> -- Howard.
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160418/6802f0ef/attachment.html>


More information about the swift-evolution mailing list