Generic functions defined in Kotlin's standard library. Since it is a generic function, it can be applied to any type. You can write more Kotlin-like code by using the scope function.
It is an image like the map function of Stream.
val str: String = "5963"
//Convert the variable str to an Int
//When using let
val i: Int = str.let { it.toInt() }
//Convert the variable str to an Int
//When using run
val i2: Int = str.run { this.toInt() }
The lambda expression that follows let
has one argument. This argument is a reference to str
.
The lambda expression that follows run
has no arguments. Instead, this reference in this lambda expression is str
.
The result of the lambda expression is the return value of let
, run
.
Also, the object that the function is called, such as the variable str
in this example, is called the ** Lacy bar object **.
Where to use
fun getName(): String? = ""
fun makeGreeting(name: String) = "Hello, $name"
fun greet(): String? {
val name = getName()
if (name == null) {
return null
}
return makeGreeting(name)
}
In the greet ()
function
Pass the result of getName ()
to themakeGreeting ()
function and
The result is the return value of the greet ()
function.
However, the return value of getName ()
is a nullable type, so a null check is required once.
Let's rewrite this using let
.
fun greet(): String? {
return getName()?.let { makeGreeting(it) }
}
Call let with a safe call to the result of getName ()
.
This way, you can write pretty neatly.
This is useful for safe calls and for passing the result of a safe cast as a function argument.
(Of course, it is often used in other cases as well.)
Similar to let and run, but the return value is always the receiver object.
val str: String = "5963"
//Standard output of variable str
//When also is used
val str2: String = str.also { print(it) }
//Standard output of variable str
//When using apply
val str3: String = str.apply { print(this) }
Where to use
class Person {
var firstName: String? = null
var middleName: String? = null
var lastName: String? = null
}
fun registerNewPerson() : Person {
val person = Person()
person.firstName = "Kumiko"
person.middleName = "Susan"
person.lastName = "Yamamoto"
return person
}
The registerNewPerson
function creates a new instance of Person
and returns it.
It just initializes Person
, but it has to be put in a variable once.
Let's use ʻalso`.
fun registerNewPerson() : Person {
return Person().also {
it.firstName = "Kumiko"
it.middleName = "Susan"
it.lastName = "Yamamoto"
}
}
It can be implemented without creating a temporary variable for Person
.
When dealing with objects that are not builder patterns, etc.
The initialization process of the object is organized in the scope function, which improves readability.
with
with is a little strange.
It's like run
, which passes a receiver object as an argument.
val str = "5963"
val i: Int = with(str) { this.toInt() }
Where to use
class Greeter {
fun greet(): String = "Hello, world"
}
fun greet(greeter: Greeter) = with(greeter) {
greet()
}
Use with to make it a single expression function. This reference will change and can be written in a different context.