Learn Kotlin Inline Functions in 3 Minutes
Tags: kotlin, android, beginners, learn
Published: 2019-02-25 16:23:19

---
title: Learn Kotlin Inline Functions in 3 Minutes
published: true
description: Learn the ins and outs of inline and how to quickly start using it in your functional Kotlin programming!
tags: kotlin,android,beginner,learn
canonical_url: https://www.trevornemanic.com/blog/2019/2/16/wip
---


Kotlin allows functions to be passed and returned from other functions. This feature is called "first class" support for functions and brings in advantages of functional programming. To learn about `inline`, we will go over the hidden costs of functional programming, see how `inline` can help reduce those costs, then wrap up with places where it should not be used.

A higher-order function comes with run-time penalties. First, memory is allocated for lambdas as function parameters. They are compiled to either `Function` classes or anonymous classes, and both cost memory. Additionally, anonymous classes are slower than `Function` classes and used only when local variables are captured. Lastly, virtual calls add unnecessary cost.

The `inline` keyword was added to Kotlin to reduce the associated costs of higher-order functions. It modifies the Java bytecode by injecting the function and its parameters directly where the function is called, the call site. Using it is easy, just add `inline` before your function:

{% gist https://gist.github.com/clevertrevor/46ca3393f57284146e40a394b25750ad %}


To see how `inline` impacts bytecode, consider two functionally equivalent higher-order functions, `filter` and `filterNotInline`. The former is part of the Kotlin standard library and the latter is a copy of it with `inline` removed.

{% gist https://gist.github.com/clevertrevor/3742f042a5de700c3e103aa82754735b %}

Decompiling this code will show how inlining works under-the-hood. Also, it was compiled to JVM bytecode and decompiled to Java for readability. Since `filter` is inlined, its code is copied directly. Here's the call site at the first `println` statement.

{% gist https://gist.github.com/clevertrevor/6df7457809c3ce4524f840dd1f854dad %}

The less efficient version requires overhead via a function call to `filterNotInline`, which contains code similar to the above.

{% gist https://gist.github.com/clevertrevor/a8bac96fc0e05aa8e5690dab7ebf51c1 %}


Inlining is usually great for performance, but there are good reasons why functions are not inlined by default:
1. Using an inlined function increases code length because the function is copied to every call site. This is especially true for long functions.
2. Inlining applies to all lambda parameters of the inlined function. So, an inlined function with three lambdas as parameters will copy them all. You can use `noinline` on individual parameters to prevent copying.
3. Regular `return` statements are not allowed. You must use a label if you wish to return from your inlined function.
4. Inline functions do not have access to data less visible than themselves.

In addition to functions, properties can be inlined too! This can be done at the individual getter and setter or the entire property.

{% gist https://gist.github.com/clevertrevor/dae3da8c65add7691873c49add161a6f %}

---

By now you should have an understanding of the benefits and tradeoffs of `inline`. Feel free to post any questions you have in the comments!
Did you like what you read? You can also [follow me on Twitter](https://twitter.com/TrevorNemanic) for more Android content.