Inline Functions in Kotlin
In Kotlin, a popular language for Android development, inline functions are crucial in optimizing higher-order functions by reducing memory overhead. Kotlin, being a statically typed language, performs most checks during compile time. Higher-order functions, typically treated as objects, can lead to runtime overhead due to memory allocation. Inline functions, declared with the "inline" keyword, mitigate this by avoiding separate memory allocation for lambda expressions. Instead, the compiler copies the function's code directly to the call site. This article delves into the mechanics of inline functions in Kotlin, illustrating their benefits and usage through examples, thereby enhancing performance and efficiency in Kotlin programming.
noinline
The noinline is a function attribute that is used to restrict some parameters of the inline function from being treated as inlined. If there are many lambdas passed into an inline function, and you want to mark some of the passed parameters as online, then we use the inline attribute. Even if the function is inline, you can declare some of the parameters as noinline using this attribute. The inline lambdas can be called inside the inline functions only. But noinline can be called in both inside as well as outside of the inside functions. You can store them in the fields or also you can pass them anywhere in your code. The noinline lambdas are declared as per the code shown below:
Let us see an example of a noinline attribute in the inline function:
Output:
Non-local returns
In the Kotlin programming language, the compiler can not return the lambda expression. Only named functions and anonymous functions in Kotlin can be terminated using a standard, unqualified return. Use a label to end a lambda. Due to the inability of a lambda to force the surrounding function to return, a plain return is not permitted inside of one. However, the return can also be inlined if the function to which the lambda is supplied is. Non-local returns are those that are contained within a lambda but leave the enclosing function.
Given below is an example to understand the non-local returns.
Output:
Crossinline Annotation
The crossline is a marker keyword that is used to restrict any return value from that expression. The lambda expression is returned from the function where that specific function was executed in a non-local return. We can mark using the crossinline marker to mark the lambda expression. Marking the lambda expression as crossinline helps to restrict return from that expression and the inline function as well. After that, if there is any return statement exists in the lambda expression, it will show an error namely "compiler error". Below is an example to understand this:
Reified Type Parameters
While working with inline functions kotlin, sometimes we need to get the type of parameter that we have used by passing them during the function call. To perform this, we use the reified modifier. To get the type of parameter using the reified modifier, we need to pass the parameter during the function call. Below is an example to perform this:
Output:
Inline properties
The inline function is used to copy the code at the calling place of the program. Just like that the inline keyword is used to make a copy of the inline properties accessor methods during the function call of the program. The inline modifier can be applied to property accessors without a supporting field. You can make a separate note for the property accessors. Below is an example to show this:
Output:
Restrictions for Public API Inline Functions
In Kotlin, if the inline function is public or private but it is not declared as private or internal, this will be treated as a public API module of kotlin. It can be used to call in other modules also. If the calling module is not recompiled after the modification, there is a danger of binary incompatibility brought on by changes in the module that declare an inline function.
To prevent this kind of incompatibility issue that is raised by doing modifications in the non-public module API, Public API inline functions are not used with the non-public API declaration. For example, declarations like private and internal and their parts.
We can use the internal declaration noted with @PublishedApi. Doing this allows us to use it in the public API of inline functions. When we declare this internal inline function as @PublishedApi, the body of the internal is also checked just like it was public.
Conclusion
- Inline functions are used to prevent memory allocation for a specific function and just copy the piece of code during the runtime of the program.
- In the Kotlin language, higher-order functions are treated as an object so that memory allocation can be done for objects and classes as well which can increase the runtime overhead.
- Inline function kotlin is declared using the keyword "inline" before declaring the function.
- We can use an Inline function Kotlin to access higher-order functions, to pass a function type parameter, to efficiently use the memory, etc.
- Inline function can be used if you want to have a better control flow of your program and you want to get rid of "object creation".
- There are some expressions and declarations which we can not use inside the inline function such as local class, nested class, functional expression, and default values for optional parameters.
- The noinline is a function attribute that is used to restrict some parameters of the function from being inlined.
- If we inline the lambda expression, the lambda will return along with the function where the inline was run and these return values are called non-local returns.
- crossinline helps to restrict return from the expression and the inline function.
- Reified type parameters are used to get the type of parameter that we have used by passing them during the function call.
- In Kotlin, if the inline function is public or private but it is not declared as private or internal, this will be treated as a public API module of kotlin.