От: | Qbit86 | https://twitter.com/qbit86 | |
Дата: | 12.11.20 14:47 | ||
Оценка: |
it: implicit name of a single parameter
It's very common that a lambda expression has only one parameter.
If the compiler can figure the signature out itself, it is allowed not to declare the only parameter and omit ->. The parameter will be implicitly declared under the name it:
ints.filter { it > 0 } // this literal is of type '(it: Int) -> Boolean'
— https://kotlinlang.org/docs/reference/lambdas.html#it-implicit-name-of-a-single-parameter
Passing trailing lambdas
In Kotlin, there is a convention: if the last parameter of a function is a function, then a lambda expression passed as the corresponding argument can be placed outside the parentheses:
val product = items.fold(1) { acc, e -> acc * e }
Such syntax is also known as trailing lambda.
If the lambda is the only argument to that call, the parentheses can be omitted entirely:
run { println("...") }
— https://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter