我有以下Kotlin代码,基于"生成操作的代码"来自here的部分:
val list = listOf(1,2,3,4,5,6)
val listRepeated = listOf(2,2,3,4,5,5,6)
println(list.merge(listRepeated) { it1, it2 -> it1 + it2 })
我收到以下错误消息:
Error:(33, 18) Kotlin: Unresolved reference: merge
Error:(33, 40) Kotlin: Cannot infer a type for this parameter. Please specify it explicitly.
Error:(33, 45) Kotlin: Cannot infer a type for this parameter. Please specify it explicitly.
如何才能正确编译此代码?
答案 0 :(得分:7)
merge
已按照here所述在Kotlin 1.0 Beta 2中删除。
正如@tim_yates所说,这段代码示例将为您提供与文章中相同的输出:
val list = listOf(1,2,3,4,5,6)
val listRepeated = listOf(2,2,3,4,5,5,6)
println(list.zip(listRepeated) { it1, it2 -> it1 + it2 })
答案 1 :(得分:4)
无法在官方文档中找到合并(但确实找到了mentioned here)
想你want zip:
println(list.zip(listRepeated) { it1, it2 -> it1 + it2 })