Why does closure support "unpacking" arguments?

Hi, can anyone explain why the test 1b works? I find it by accident when I modify my code. I didn't expect it work, because I never read that Swift supports packing and unpacking (the test 2 below verified it).

func test1(_ c: ((Int, Int)) -> Void) {
    let data = (1, 2)
    c(data)
}

// test 1a
test1 { tuple in
    let (x, y) = tuple
    print("x: \(x), y: \(y)")
}

// test 1b: this works
test1 { x, y in
    print("x: \(x), y: \(y)")
}

func test2(_ x: Int, _ y: Int) {
    print("x: \(x), y: \(y)")
}

// test 2: this doesn't compile
test2((1, 2))

The relevant thread is here:

2 Likes