[swift-evolution] Proposal for Passing Arrays to Variadic Functions

Chris Lattner clattner at apple.com
Sun Dec 13 17:00:25 CST 2015


> On Dec 11, 2015, at 10:54 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
> 
> You can't use * as suggested, because that conflicts with * as a prefix operator.
>  
> You could probably get away with using a trailing ... in the call, as in
>  
> func foo(xs: [Int]...) {
>     bar(xs...)
> }
>  
> I also wonder whether there's a reason Swift doesn't already support this. Maybe there's some desired future change to variadic arguments to improve performance (i.e. to avoid having to allocate an array) that wouldn't be compatible? Although I'm not sure what that would be. Maybe there's actually no barrier at all and it was just something that wasn't prioritized. It would be good to know either way.

This hasn’t been done just because it has never risen to the highest priority, not because we’re against it.  In addition to Joe Groff’s downstream comment, one change we need to discuss: requiring the formation of a formal Array is somewhat unfortunate from a performance perspective.  It would make more sense to pass varargs by allocating the array as a fixed size buffer in the callers stack, and passing a pointer+length down to the callee.  The callee could then wrap that up and vend it with a formal type of Array or perhaps just SequenceType.  In the case where the sequence doesn’t escape, this would allow the allocation/deallocation + copy to the heap to be eliminated.

-Chris



More information about the swift-evolution mailing list