Swift: Unable to decompose tuple in certain closures (e.g., reduce with enumerate)?

一笑奈何 提交于 2019-12-03 22:50:02

问题


When using map() with enumerate(), Swift will decompose the enumerate tuple:

map(enumerate([1,2,3])) { (index, element) in
    index + element
}

However, this does not appear to work alongside an additional closure parameter (e.g., with reduce()):

reduce(enumerate([1,2,3]), 0) { (accum, (index, element)) in
    accum + index + element
}

This fails with error: use of undeclared type 'index'.

Am I missing something simple, or does Swift simply not allow decomposing a tuple alongside an additional parameter? I have tried this in 1.1 and 1.2. (For now, I am using the shorthand argument names.)


回答1:


The answer is "Swift simply not allow it". You can't decompose nested tuple like that.

For example, this code compiles, but we cannot access j or k:

func foo(i: Int, (j: Int, k: Int) ) {
    // println(j)
    //         ^ error: use of unresolved identifier 'j'
}
foo(1, (2, 3))

Because, in this code, (j: Int, k: Int) is just a type, and the received tuple itself does not have a name. You have to write like this instead:

func foo(i: Int, x: (j: Int, k: Int) ) {
    println(x.j)
}
foo(1, (1, 2))

In the same way, this compiles, but it's useless.

reduce(enumerate([12,42,84]), 0) { (accum, (index:Int, element:Int)) in

Instead, you have to receive the tuple itself, then decompose it if you want:

reduce(enumerate([12,42,84]), 0) { (accum, enumerated)  in
    println(enumerated.element)

    let (index, element) = enumerated


来源:https://stackoverflow.com/questions/28924252/swift-unable-to-decompose-tuple-in-certain-closures-e-g-reduce-with-enumerat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!