Kotlin 参考嵌套循环中的外部$it

Kotlin 参考嵌套循环中的外部$it,kotlin,Kotlin,我的代码如下: tt@repeat(5) { repeat(5) { // Here refer to $it from outer repeat, // as well as from inner one print("${tt@it} $it,") } println() } 上述代码不起作用,并生成警告: 标签是冗余的,因为它不能在

我的代码如下:

    tt@repeat(5) {
        repeat(5) {
            // Here refer to $it from outer repeat, 
            // as well as from inner one
            print("${tt@it} $it,")
        }
        println()
    }
上述代码不起作用,并生成警告:

标签是冗余的,因为它不能在“break”、“continue”或“return”表达式中引用


是否可以不在某些局部变量中存储外部$it?

是的,您可以这样修改代码:

 tt@repeat(5) { _it1 ->
    repeat(5) {
        // Here refer to $it from outer repeat, 
        // as well as from inner one
        print("$_it1 $it,")
    }
    println()
}

不需要
tt@
。此外,变量名可能更好,例如:``重复(5){rowVal->重复(5){colVal->print(“$rowVal$colVal,”)}println()}```