Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the `it` in Kotlin lambda body?

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?

like image 999
Sujin Shrestha Avatar asked Jun 27 '17 06:06

Sujin Shrestha


People also ask

What is the IT in Kotlin?

Here is the kotlin org doc: it: implicit name of a single parameter. for example ints.filter { value -> value > 0 }

What is a lambda expression when is the keyword it used in lambda in Kotlin?

Lambda expression is a simplified representation of a function. It can be passed as a parameter, stored in a variable or even returned as a value. Note: If you are new to Android app development or just getting started, you should get a head start from Kotlin for Android: An Introduction.

What is () -> unit in Kotlin?

Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

How do you define a lambda in Kotlin?

Lambda is a function which has no name. Lambda is defined with a curly braces {} which takes variable as a parameter (if any) and body of function. The body of function is written after variable (if any) followed by -> operator.

What is Lambdas in Kotlin?

Kotlin Lambdas. Lambda Expressions. Lambda expression or simply lambda is an anonymous function; a function without name. These functions are passed immediately as an expression without declaration. For example,

What is simplelambda in Kotlin?

Because there are no argument types to declare, and the return value may be inferred from the lambda body, we may simplify this lambda even further. Now we are relying on Kotlin’s type inference engine to infer that simpleLambda is a function that takes no arguments and returns Unit.

What is lambda expression in koltin?

Here, the lambda expression is: Note, a lambda expression is enclosed inside curly braces. Koltin has a great support for functional programming. You can pass functions as arguments to other functions. Also, you can return a function from other functions.

What is a function in Kotlin?

To facilitate this, Kotlin, as a statically typed programming language, uses a family of function types to represent functions, and provides a set of specialized language constructs, such as lambda expressions. A higher-order function is a function that takes functions as parameters, or returns a function.


2 Answers

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:

like image 141
holi-java Avatar answered Oct 11 '22 04:10

holi-java


Please refer to the following description.

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

like image 20
Jaemoon Hwang Avatar answered Oct 11 '22 05:10

Jaemoon Hwang