例如在下面这两个代码中:
File("./file1.txt").forEachLine { println(it) }
和
File("somefile.txt").bufferedWriter().use { out ->
history.forEach {
out.write("${it.key}, ${it.value}\n")
}
}
在这段代码中,it
是什么意思?
最佳答案
it variable 是 lambda 中的隐式参数。
One other helpful convention is that if a function literal has only one parameter, its declaration may be omitted (along with the ->), and its name will be it:
关于java - Kotlin lambda 主体中的 `it` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773351/