Why doesn't Swift's closing syntax allow return on implicit parameter?
What is the rationale behind Swift language design to make the following acceptable
[1, 2, 3, 4].map({ (number:Int)->Int in return number * 3 }) // ok
or
[1, 2, 3, 4].map({ number in number * 3 }) // ok
or
[1, 2, 3, 4].map({ 3 * $0 }) // ok
making it unacceptable?
[1, 2, 3, 4].map({ return 3 * $0 }) // not ok
+3
source to share
3 answers
This is clearly a compiler error, because moving the closure to a separate variable works:
let closure1: (Int) -> (Int) = { return 3 * $0 }
var closure2: (Int) -> (Int) = { return 3 * $0 }
[1, 2, 3, 4].map( closure1 ) // Works
[1, 2, 3, 4].map( closure2 ) // Works
[1, 2, 3, 4].map( { return 3 * $0 } ) // Fails
+3
source to share