> Le 7 juin 2017 à 12:03, Adrian Zubarev via swift-evolution > <swift-evolution@swift.org> a écrit : > > Well please no: > > > let fn2: ((Int, Int)) -> Void = { lhs, rhs in } > > Instead use destructuring sugar pitched by Chris Lattner on the other thread: > > let fn2: ((Int, Int)) -> Void = { ((lhs, rhs)) in } >
Despite Chris Lattern being a semi-god, his double-parenthesis suggestion cruelly lacks in terms of user ergonomics. The compiler should be able to deal with the following code snippet, just like Swift 3 does: // two arguments func f1(_ closure: (Int, Int) -> Int) { closure(1, 2) } f1 { lhs, rhs in lhs + rhs } f1 { (lhs, rhs) in lhs + rhs } f1 { tuple in tuple.0 + tuple.1 } f1(+) // two arguments, with documentation names func f2(_ closure: (_ a: Int, _ b: Int) -> Int) { closure(1, 2) } f2 { lhs, rhs in lhs + rhs } f2 { (lhs, rhs) in lhs + rhs } f2 { tuple in tuple.0 + tuple.1 } f2(+) // one tuple argument func f3(_ closure: ((Int, Int)) -> Int) { closure((1, 2)) } f3 { lhs, rhs in lhs + rhs } f3 { (lhs, rhs) in lhs + rhs } f3 { tuple in tuple.0 + tuple.1 } f3(+) // one keyed tuple argument func f4(_ closure: ((a: Int, b: Int)) -> Int) { closure((a: 1, b: 2)) } f4 { lhs, rhs in lhs + rhs } f4 { (lhs, rhs) in lhs + rhs } f4 { tuple in tuple.a + tuple.b } f4(+) This covers the Swift 3 regressions developped by Stephen Celis and I, unless I have missed any. And this should be the goal of the designers of this language, *regardless of the actual types*. Developers are olding their breath: please just make this happen. Now Swift 3 may have an issue with $n parameters. Here is my suggestion, should `((Int, Int)) -> Int` be different from `(Int, Int) -> Int`: f1 { $0 + $1 } // OK f1 { $0.0 + $0.1 } // OK in Swift 3, compiler error in Swift 4? f2 { $0 + $1 } // OK f2 { $0.0 + $0.1 } // OK in Swift 3, compiler error in Swift 4? f3 { $0 + $1 } // OK in Swift 3, compiler error in Swift 4? f3 { $0.0 + $0.1 } // OK f4 { $0 + $1 } // OK in Swift 3, compiler error in Swift 4? f4 { $0.a + $0.b } // OK Gwendal Roué
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution