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 Person
object and apply
set the object's properties in the function. What is ultimately returned is the Person
object itself.
3. let
let
It is also an extension function that also receives a Lambda expression as a parameter. Unlike apply
in a Lambda expression, let
the function takes the receiver object as a parameter of the Lambda, usually used it
as an implicit name. let
The 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, let
the function is used to calculate Person
the object's age in five years.
Four, also
also
is an extension function, apply
similar to , but with one key difference: also
the return value of the function is the receiver object itself, while the parameter of the lambda expression is the receiver object, often used it
as 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, also
the function is used to modify Person
the properties of the class and finally returns the modified object.
5. run
run
is an extension function that combines the characteristics of apply
and let
. run
The 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, run
the function is used to generate a Person
string 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.