[Android] What are the differences between apply, let, with, also, and run in Kotlin?

1. Illustration 

2. apply

The apply function takes an object and returns the object itself. It allows you to perform some operations on an object while still returning the original object.

The syntax of this function is:

fun <T> T.apply(block: T.() -> Unit): T

Among them, T is the type of the object, and block is a lambda expression that can perform some operations on the object. Within this lambda, you can use the this keyword to refer to the current object.

For example:

val person = Person().apply { name = "wsy" age = 20 address = "HangZhou" }

In this example, we create an Personobject and applyset the object's properties in the function. What is ultimately returned is the Personobject itself.

3. let

letIt is also an extension function that also receives a Lambda expression as a parameter. Unlike applyin a Lambda expression, letthe function takes the receiver object as a parameter of the Lambda, usually used itas an implicit name. letThe return value of the function is the result of the Lambda expression.

The syntax of this function is:

kotlinCopy code fun <T, R> T.let(block: (T) -> R): R

Among them, T is the type of the object, R is the type of the return value, and block is a lambda expression that performs some operations on the object and returns a result.

val person = 
    Person("Alice", 25) 

val ageAfterFiveYears = 
    person.let { it.age + 10 }

In this example, letthe function is used to calculate Personthe object's age in five years.

Four, also

alsois an extension function, applysimilar to , but with one key difference: alsothe return value of the function is the receiver object itself, while the parameter of the lambda expression is the receiver object, often used itas the implicit name. The syntax of this function is:

fun <T> T.also(block: (T) -> Unit): T

Among them, T is the type of the object, and block is a lambda expression that can perform some operations on the object. In this lambda, you can use the it keyword to refer to the current object.

val person = Person("XiaoYan", 18).also { it.name = "Wsy" it.age = 20 }

In the above example, alsothe function is used to modify Personthe properties of the class and finally returns the modified object.

5. run

runis an extension function that combines the characteristics of applyand let. runThe function directly accesses the properties and methods of the receiver object in the Lambda expression and returns the result of the Lambda expression.

The syntax of this function is:

fun <T, R> T.run(block: T.() -> R): R

Among them, T is the type of the object, R is the type of the return value, and block is a lambda expression that performs some operations on the object and returns a result. Within this lambda, you can use the this keyword to refer to the current object.

val person = Person("Wsy", 25) 
val greeting = person.run { "Hello, $name! You are $age years old." }

In this example, runthe function is used to generate a Personstring containing object information.

6. Summary

What the four functions have in common is that they can all operate on objects and can reference the current object in the lambda. However, their return values ​​and return timing are different.

The return value of the apply and also functions is the object itself, while the return value of the let and run functions is the result of the lambda expression.

The apply function performs some operation on an object and returns the object itself. It is typically used to initialize an object immediately after it is created.

The also function is similar to the apply function, but it returns a reference to the original object. It is usually used to perform some side effects on the object, such as printing logs or modifying the object state.

The let function performs some operation on the object in the lambda and returns the result of the lambda expression. It is usually used to perform transformations or calculations on objects under certain conditions.

The run function is similar to the let function, but it returns the result of a lambda expression. It is usually used to perform calculations on objects and return the calculation results.

Taken together, these four functions are very useful functional programming tools that help you manipulate objects and blocks of code in a concise, readable way. For each situation, you should choose the most appropriate function so that you can write your code in the most efficient way.

 

Guess you like

Origin blog.csdn.net/qq_41264674/article/details/133277447