For example in these two following codes:

File("./file1.txt").forEachLine { println(it) }

and

File("somefile.txt").bufferedWriter().use { out ->
    history.forEach {
        out.write("${it.key}, ${it.value}\n")
    }
}

In this code what does it mean?

推荐答案

it变量是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:

Kotlin相关问答推荐

判断字符串是否除了.&" ",","@""""

如何在 kotlin 中的数据类中为变量提供多种类型

在 map 中查找键与在 kotlin 中查找 firstOrNull

如何在 Kotlin 中为类方法调用传递变量

为什么 Kotlin 在将协变类型参数转换为不变类型参数时会产生 Unchecked Cast 警告?

mutableStateOf 和 mutableStateListOf 有什么区别?

验证构造函数中的值组合

顶级属性的初始化

比较 Kotlin 中的可比对象列表

Kotlin 是如何编译的?

使用 Kotlin 创建新目录,Mkdir() 不起作用

Kotlin 中的内联构造函数是什么?

无法在Kotlin中使用argb color int值?

如何解决:将Java类转换为Kotlin后出现error: cannot find symbol class ...?

使用范围的稀疏sparse值列表

Kotlin - 是否可以在类中的 init 块之前初始化伴随对象?

如何将vararg作为数组传递给Kotlin中的函数?

如何在 Gradle Kotlin DSL 中使用来自 gradle.properties 的插件版本?

为什么 Kotlin 会收到这样的 UndeclaredThrowableException 而不是 ParseException?

如何在 Kotlin 中将串联转换为模板