Consider the following set of functions:
func testFunc(someFunc: (Int[]) -> ()) {
someFunc([1, 2, 3])
}
func someFunc<T>(arr : T[]) -> T[] {
return arr
}
func someOtherFunc<T>(arr : T[]) {
println(arr)
}
// case 1 - ERROR
testFunc() {
someFunc($0)
}
// case 2 - no error
testFunc() {
println("whatever")
someFunc($0)
}
// case 3 - no error
testFunc() {
someOtherFunc($0)
}
It looks like in case 1, Swift is trying to implicitly return from the closure since the function someFunc()
returns a value. It only does this if there is only one line in the closure (Implicit Returns from Single-Expression Closures) - this is why case 2 works. It does not do this if the function, as in case 3 is Void
, i.e. it doesn't return a value.
My question is whether there is a way to suppress this behavior so that I can have a function with a return value as a single-line expression in a closure that has no return value.