Free AI web copilot to create summaries, insights and extended knowledge, download it at here
1702
Abstract
e useful to shorten long verbose types into something more manageable, as well as giving meaning to nonspecific types. <code>Pair<Long, Int></code> could be <code>OnlineUserCountAtInstant</code>, for example.</p>
<figure id="1ac2">
<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%2FcCgNeHkBl&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FcCgNeHkBl&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="0252">A proper choice of type alias can have a profound influence on how readable your code is.</p><p id="fd8b">For example:</p>
<figure id="48c2">
<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%2FphPO_u7fF&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FphPO_u7fF&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="a47a">However, it is extremely important to keep in mind that <b>type aliases aren
Options
’t new types!</b> The compiler always expands them to the type they represent, which can lead to mistakes if used improperly:</p>
<figure id="ff59">
<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%2FAPtcUt0ED&display_name=Kotlin+Playground&url=https%3A%2F%2Fpl.kotl.in%2FAPtcUt0ED&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="cd8c">Since both <code>firstname</code> and <code>lastname</code> are (type aliased) strings, there’s nothing stopping you from accidentally flipping the two.</p><p id="9cf6">If you want to prevent errors like this, the next chapter’s got you covered.</p><p id="c99c">Go back to <a href="https://readmedium.com/operators-6625ca0b8a63">Operators</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/inline-value-classes-773093aece23">Inline (Value) Classes</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>