-1

Refurbished Question

Swift: Combining the use of Variadic Parameters with Closures


Is it possible to make a closure using variadic parameters?

func this(_ hello: (Int...) -> ()) {}
func that(_ yeah: Int...) {}

Here's what works: this(that)
Here's what didn't work: this { foo in }, this { _ in }, this { }


I'm getting this error:
Cannot convert value of type '(_) -> ()' to expected argument type '(Int...) -> ()'


I even went so far as to change all Int... to Void...
I'm got the same results
With the additional warning of When calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'? Replace '(Void...)' with '()' However, replacing Void... with ()... did the trick


Simplified Problem


let _: (()...) -> () = { _ in }

Causes this error:
Cannot convert value of type '(_) -> ()' to specified type '(()...) -> ()'
Is it possible to work around this?

Community
  • 1
  • 1
0-1
  • 702
  • 1
  • 10
  • 30

1 Answers1

2

You need to include the type in the closure:

this { (foo: Int...) in }

The type-inference engine isn't quite powerful enough to figure out this case. I suspect it's related to SR-6030.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610