avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2503

Abstract

c="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FYQ4XBAbEf&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FYQ4XBAbEf&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="43fc">In this case, whenever <code>map</code> is called, a lambda object must be created with all the overhead described before. However, we would be perfectly fine if, instead, the definition of the <code>map</code> function was just "copy-pasted" (i.e. inlined) where it was called. In essence, what we want the above call to mean is (roughly):</p> <figure id="55c3"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F0ie2ARZ9y&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F0ie2ARZ9y&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="1420">Naturally, that’s not <i>exactly</i> what will happen (e.g. we don’t want the <code>result</code> variable to pollute our scope), but roughly speaking, that's what we mean - we just don't want to write it out every time.</p><p id="8f1f">This is precisely what the <code>inline</code> keyword is for:</p> <figure id="9b76"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Fzil2aC6Rl&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Fzil2aC6Rl&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="e4e3">I know we haven’t covered these things yet, but just so you know, you can do the same things wi

Options

th <a href="https://readmedium.com/backing-fields-e0efd8ef6895"><i>properties without backing fields</i></a>:</p> <figure id="9fe0"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FZaLlPWOpX&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FZaLlPWOpX&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="44f4">It’s probably kinda-sorta-intuitively understandable what we’re doing here, and we’ll cover the details very soon. Just make a mental note and don’t worry about it for now.</p><p id="c040">There are some technicalities that sometimes need to be taken care of with the <code>crossinline</code> keyword, but we won't go into those - feel free to peruse the <a href="https://kotlinlang.org/docs/inline-functions.html">documentation on inline functions</a>.</p><p id="d793">We’ll revisit the topic of inline functions again when we talk about <a href="https://readmedium.com/reified-type-parameters-dd70e7cc4ee1"><i>reified type parameters</i></a> in later sections, another very cool Kotlin feature.</p><p id="c8ed">In practice — <b>whenever you define a higher-order function, always try adding the inline keyword</b>. If it works, excellent, you just made your code more performant. The compiler will always let you know when you need to add the <code>crossinline</code> keyword, or when you do something that's not allowed.</p><p id="70c7">Go back to <a href="https://readmedium.com/function-types-literals-f384725e2fdd">Function Types & Literals</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/tail-recursive-functions-a9fcc95fb8ef">Tail Recursive Functions</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Inline functions

Inline functions in Kotlin and when to use them, inline getters and setters, and crossinline.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Using higher order functions will quickly become second nature to you, especially once you find out for yourself how powerful this programming style is. However, there ain’t no such thing as a free lunch — if you’re writing performant code, lambdas are expensive.

Each function is an object, and it captures a closure, i.e. the variables that are defined outside the function, which are accessed in the body of the function. Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead.

Thankfully, in many cases this kind of overhead can be eliminated by inlining the lambda expressions.

Take the definition of the map function:

In this case, whenever map is called, a lambda object must be created with all the overhead described before. However, we would be perfectly fine if, instead, the definition of the map function was just "copy-pasted" (i.e. inlined) where it was called. In essence, what we want the above call to mean is (roughly):

Naturally, that’s not exactly what will happen (e.g. we don’t want the result variable to pollute our scope), but roughly speaking, that's what we mean - we just don't want to write it out every time.

This is precisely what the inline keyword is for:

I know we haven’t covered these things yet, but just so you know, you can do the same things with properties without backing fields:

It’s probably kinda-sorta-intuitively understandable what we’re doing here, and we’ll cover the details very soon. Just make a mental note and don’t worry about it for now.

There are some technicalities that sometimes need to be taken care of with the crossinline keyword, but we won't go into those - feel free to peruse the documentation on inline functions.

We’ll revisit the topic of inline functions again when we talk about reified type parameters in later sections, another very cool Kotlin feature.

In practice — whenever you define a higher-order function, always try adding the inline keyword. If it works, excellent, you just made your code more performant. The compiler will always let you know when you need to add the crossinline keyword, or when you do something that's not allowed.

Go back to Function Types & Literals, jump to the Table of Contents, or continue to Tail Recursive Functions.

Join me in Etnetera
Kotlin
Java
Programming
Inline
Higher Order Function
Recommended from ReadMedium