What is the `it` in Kotlin lambda body?

Sujin Shrestha :

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?

holi-java :

it variable is an implicit parameter in 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:

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421883&siteId=1