Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3069
Abstract
<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%2FnN9trogjq&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FnN9trogjq&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="cf3a">The issue here is almost identical — inside the <code>runOnA</code> block, we have <i>two</i> candidate values for <code>this</code> — the <code>block</code> is a function with receiver of type <code>A</code>, so an <code>A</code> instance is in scope inside the literal, but <code>block</code> is also defined as part of an extension function on <code>B</code>, which means that we have a <code>B</code> instance in scope as well.</p><h2 id="05ec">Qualified this</h2><p id="282f">This is identical to the situation encountered with <a href="https://readmedium.com/nested-and-inner-classes-b300243d036d">inner classes</a>, and the solution is also identical: <b>if not qualified, a <code>this</code> expression refers to the innermost enclosing scope</b>. If we want to refer to a different one, we need to qualify the <code>this</code> expression with a label by writing <code>this@label</code>.</p><p id="14b7">Implicit labels are created for every class body and function <b>statement</b> (not literal) body. Here is an example describing all the possibilities:</p>
<figure id="03ce">
<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%2FjOKLGWp-s&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FjOKLGWp-s&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="78ac">This solves our problem when dealing with the receivers of statements (class and function definitions), but it still doesn’t help us with literals (anonymous functions & lambdas with or without receivers). However, as was mentioned in the chapter on <a href="https://readmedium.com/nested-and-inner-classes-b300243d036d">inner classes</a>, (explicit) <b>labels can also be used to explicitly qualify expressions</b>. You can use that to your advantage when dealing with conflicting receivers inside literals:</p>
<figure id="1fac">
<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%2FTrIoxmsbt&display_name=Kotlin+P
# Options
layground&url=https%3A%2F%2Fpl.kotl.in%2FTrIoxmsbt&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><h1 id="d1cb">Exercises</h1><p id="50db">It’s brain-fuck time! Spend some time thinking about what the <code>abc</code> function prints. Remember that <code>-></code> is right associative, so the type bellow
is the same as <code>A.() -> (B.() -> (C.() -> Unit))</code>.</p><p id="7cef">Start reading: "The function 'abc', when invoked, returns a function
with receiver <code>A</code>, which, when invoked, returns a function...". Contrast this with the number of invocations in main.</p><p id="121f">Think about what this function prints when run in its current form. Run the example — were you right?</p>
<figure id="37e3">
<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%2FMDyD-OHuF&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FMDyD-OHuF&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="890f">Make changes so it evaluates to <code>A B C</code>:</p>
<figure id="29e8">
<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%2FBfF45HfYt%3Ffrom%3D20%26to%3D29&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FBfF45HfYt%3Ffrom%3D20%26to%3D29&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="a560">Go back to <a href="https://readmedium.com/functions-with-receiver-28b71b11ddde">Functions with Receiver</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/what-extension-functions-are-not-3c9fe874ed3a">What Extension Functions Are Not</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>