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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FYQ4XBAbEf&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2F0ie2ARZ9y&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2Fzil2aC6Rl&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FZaLlPWOpX&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&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&utm_medium=GabrielShanahan&utm_campaign=KotlinPrimer&utm_content=join-our-team&utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>