[Discussion] Forwarding of variadic arguments

Currently it's not possible to wrap a function that takes variadic arguments because it's not possible to pass multiple variadic values to that function. Look at this example:

// This function exists and can't be changed
func someExistingImportedFunction(_ specialArgument: Int, _ args: Int...) {}

// This function should just be a helper one to wrap the existing
func someFunctionThatWrapsTheOtherOne(_ args: [Int]) {
    someExistingImportedFunction(1, args) // error: cannot convert value of type '[Int]' to expected argument type 'Int'
}

I know that it would be possible to create a variant of someExistingImportedFunction that takes [Int] instead of Int... but as the name says, it's imported and can't be changed.
I know that there is a similar discussion about this topic from 2016 here but that went in a different direction to remove variadic arguments completely. I don't see this as the useful solution here because variadic arguments are an important feature of the language and that change would break a lot of existing code. (Also: existing Jira-Issue)

I think a better solution would be to add syntax to split arrays to variadic arguments. Go (and other languages) offer this as well with syntax like *arr or ...arr. The ... syntax would fit better to Swift but could also confuse because it's used for open ranges as well.

What do you think about this in general? Do you think it could be a useful addition? If yes, what are your proposals for syntax? Really excited about the feedback. Here are two examples that use variadic arguments that can't be wrapped currently: String(format:), os_log

  • Ben
3 Likes

Hello, it looks like you could join and revive this recent thread: Explicit array splat for variadic functions

1 Like

Thanks, I was searching for variadic forwarding and didn't find this one.

1 Like