[swift-evolution] Revisiting SE-0110

Robert Bennett rltbennett at icloud.com
Fri May 26 09:31:35 CDT 2017

I've tried to follow this thread, but forgive me if I'm suggesting something that's already been mentioned. Could we allow tuple destructuring by wrapping the inner tuple in parentheses in the closure?

var d = [String: Int]()
// Do stuff with d

d.forEach { (key, value) in ... } // Illegal; argument to closure is a 2-tuple, not two arguments

d.forEach { (arg) in // Works, but ugly
    let key = arg.key
    let value = arg.value

Now, based on the above, I'm proposing that this should work:

d.forEach { ((key, value)) in ... } 

Since arg == (key, value), "(arg) in" should be equivalent to "((key, value)) in". In light of this, "{ arg in ..." would merely be syntactic sugar for "{ (arg) in ...", since 1-tuples don't actually exist (`let f: (Int) = 1; print(f.0)` doesn't compile, and `let f: ((Int, Int)) = (1,2); print(f.0)` prints `1`, not `(1, 2)`).

More information about the swift-evolution mailing list