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


This looks like a bug in the type inference engine. For example:

[1, 2, 3].map({ return 3 * $0 } as Int -> Int)

      



fine.

Edit: Removed some nonsense thanks to @Antonio's comment.

+2


source


I suspect this is a beta6 compiler bug. Note that the following works (adding an explicit type):

let z:[Int] = [1, 2, 3, 4].map({ return 3 * $0 })

      

I would check this again in the next beta.

+2


source







All Articles