Android

Kotlin inline function and infix notation

Kotlin language includes many new features that allow writing cleaner, easier-to read code. This makes our code significantly easier to maintain and allows for a better end result from our development.

Such features include

• Kotlin inline function

• Kotlin infix notations

Android Training in STEPS including the following features of Kotlin in Android Development to make your application more advanced with its features and user-friendly.

What are inline functions?

Kotlin inline functions are declared with the keyword of inline and is used to enhance the performance of higher order functions. In order to reduce the memory with higher order and lambda functions we can use inline keyword which request the compiler to not allocate memory and simply copy the inlined code of the function to the coding place.

The virtual function or the local functions may not be the inline function. Because it does not support the declaration of local classes, inner nested classes, Function expressions, local function and cannot set default value for optional parameters.

The inline functions can return from the lambda expression itself, this will lead to exit from the function when it called. To prevent this, we can mark the lambda expression as a crossinline.

What is an infix notation?

In kotlin we can call a function without using periods and brackets .These are called infix methods.

There are two types of infix function notations

Standard library infix function notation

While using the operators like and, or, shr, shl etc the compiler look for the functions and then call the desired one.

The User defined infix function notation

User can also create our own infix functions by satisfying some conditions like

  • It must be an extension function or a member function
  • It must be accepted single characters
  • The parameters may not be a variable number of arguments and must have a default value
  • It must be marked with infix keyword

Author: STEPS

Leave a Reply

Your email address will not be published. Required fields are marked *