Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of inline function in Kotlin

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of inline functions in Kotlin. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Understanding of inline function

Inline function (inline function) is conceptually that the compiler uses the real code implemented by the function to replace each function call. The most direct benefit is to save the cost of function calls, while the disadvantage is to increase the size of the generated bytecode. Based on this, is it necessary for us to define all functions as inline when the amount of code is not very large? Let's explain it in two situations:

Define an ordinary function as inline: it is well known that inline optimization has been implemented within JVM, which inlines function calls wherever performance can be improved by inlining, and instead of manually defining ordinary functions as inline, the bytecode generated by JVM inline optimization occurs only once, which ensures that the runtime overhead is reduced without increasing the size of the bytecode So we can conclude that for ordinary functions, it is not necessary to declare them as inline functions, but to leave it to JVM to optimize itself.

Define a function with a lambda parameter as inline: yes, it does improve performance in this case, but in the process of using it, we will find that it has many limitations, so let's start with the following example:

Inline fun doSomething (action: ()-> Unit) {println ("Before doSomething...") Action () println ("After doSomething...")}

If we call doSomething like this:

Fun main (args: Array) {doSomething {pringln ("Hello World")}}

The above call is compiled into:

Fun main (args: Array) {println ("Before doSomething...") Println ("Hello World") println ("After doSomething...")}

As you can see from the compilation above, both the doSomething function and the action parameter are inlined, which is great, so let's call it in a different way:

Fun main (args: Array) {val action: ()-> Unit = {println ("Hello World")} doSomething (action)}

The above call is compiled into:

Fun main (args: Array) {println ("Before doSomething...") Action () println ("After doSomething...")}

The doSomething function is inlined, but the action parameter is not inlined, because the lambda passed to doSomething as a functional variable is not available at the point of call of the function, and the lambda can be used normally only after doSomething is inlined.

Using the above example, we make a simple summary of when lambda expressions are inlined:

When the lambda expression is passed directly to the inline function as an argument, the code of the lambda expression is replaced directly into the resulting code.

When the lambda expression is saved somewhere and then passed to the inline function as a variable, the code for the lambda expression will not be inlined.

The inline timing of lambda is discussed above, and after digesting it for a while, let's look at the last example:

Inline fun doSomething (action: ()-> Unit, secretAction: ()-> Unit) {action () doSomethingSecret (secretAction)} fun doSomethingSecret (secretAction: ()-> Unit) {}

Is there a problem with the above example? Yes, the compiler throws a "Illegal usage of inline-parameter" error because Kotlin specifies that the lambda parameter in an inline function can only be called directly or passed to another inline function, and cannot be used for other purposes; what if we do want to pass a lambda to a non-inline function? We just need to modify the above code like this:

Inline fun doSomething (action: ()-> Unit, noinline secretAction: ()-> Unit) {action () doSomethingSecret (secretAction)} fun doSomethingSecret (secretAction: ()-> Unit) {}

Quite simply, add the noinline modifier before the inline lambda parameter is not needed.

This is my full understanding of inline functions. By mastering the operating mechanism of this feature, I believe you can use it at the right time, rather than abusing it or abandoning it out of fear.

This is the end of this article on "sample analysis of inline functions in Kotlin". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report