Tutorial on how to use common inline extension functions in kotlin

preface

Kotlin's strength lies in its extension functions. Skillfully using these extension functions can make your code more elegant and read more smoothly. The following summarizes some inline extension functions often used in development. There are often little friends who don't understand how and where to use the functions such as with, run and apply. My suggestion is to remember the functions of each function (nothing more than what parameters it needs and what the return value is?) remember these two points, and then use them skillfully according to the actual development scenarios. In fact, these functions are very similar. Different functions can complete the same functions, as can be seen from the following examples. In my previous development experience, there are two main use scenarios for these functions, one is non empty judgment, and the other is object initialization or frequent calls of itself and methods.

The difference between inline and normal functions is not the similarities and differences of defined functions. When defining, you only need to add an identifier to make the normal function become an inline function. The difference between the two is in the processing mechanism during actual execution. Inlining has low consumption performance and less stack pressing and out of stack operations than normal functions. It is a way of exchanging space for time. When there are few function bodies and frequently called functions, they are suitable to be defined as inline functions

1. with

Definition: Fun < T, R > with (receiver: T, block: T. () - > R): R

Function: take the object as the parameter of the function. You can refer to the object through this in the function. The return value is the last line of the function or the return expression.

example:

1. In the custom view, when we initialize the brush, we often write the next code

If you use with, you can write it like this

It feels more natural to write without paint

2. In the scenario of declaring some sets, for example:

With can be written as

There are still many scenarios in development that can use the function of with. If you understand the function of with, you can use it flexibly.

2. Takeif and takeunless

takeif

Definition: Fun < T > t.takeif (predict: (T) - > Boolean): t?

Function: pass a function parameter. If the function result is true, return t object; otherwise, return null.

Example: when using a file, you usually judge whether the file exists, such as

After using takeif

takeUnless

Definition: Fun < T > t.takeunless (predict: (T) - > Boolean): t?

Function: Contrary to takeif, the parameter function returns t object when it returns false, otherwise it returns null. There is no example here.

3. run

definition:

(1)fun <R> run(block: () -> R): R

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

Function: call the run function, and the return value is the last line of the function body or the return expression.

example:

Returns the last line

result:

I/System.out: 11

I/System.out: 22

Return the return expression. The code after return will no longer be executed (pay attention to the writing @ run)

result:

I/System.out: 11

4. repeat

Definition: Fun repeat (Times: int, action: (int) - > unit)

Function: repeat the action function times times times, starting from 0

Example: similar to the function of the for loop, for example

Equivalent to

perhaps

5. let

Definition: Fun < T, R > t.let (block: (T) - > R): R

Function: call the let function of the object (T), then the object is the parameter of the function. You can refer to the object through it within the function. The return value is the last line of the function or specifies a return expression.

Example: somewhat similar to run (), let can be used for null security verification in use, and the variable let{}

for example

6. apply

Definition: Fun < T > t.apply (block: T. () - > unit): t

Function: call the apply function of the object. Within the scope of the function, you can call any method of the object and return the object.

example:

Note: it is different from the run function. Run returns the last line and apply returns the object itself. From the definition of the apply function, we can see that apply is applicable to those objects whose attributes need to be assigned during initialization.

Or the example of using a brush

original

After using apply

In addition, since the apply function returns the object itself, it can cooperate with Complete the multi-level non empty judgment operation, or use it in the builder of the builder mode

7. also

Definition: Fun < T > t.also (block: (T) - > unit): t

Function: call the object's also function. In the function block, you can refer to the object through it, and the return value is the object itself. (note the difference between let and let. Let returns the last line, and also returns the object itself.)

Example: when the object itself (this) needs to be returned, such as builder mode.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>