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?
Here is the kotlin org doc: it: implicit name of a single parameter. for example ints.filter { value -> value > 0 }
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.
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.
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.
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,
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.
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.
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With