avatarGabriel Shanahan

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&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FcCgNeHkBl&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="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&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FphPO_u7fF&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="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&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FAPtcUt0ED&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="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&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>

Type Aliases

Introducing type aliases, including generic variants, what they are and what they aren’t, and how a simple type alias can have a profound influence on how readable your code is.

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

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

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

Tags: #KOTLIN FEATURE

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.

Kotlin allows you to define type aliases, which are simply placeholders for other types.. These can be useful to shorten long verbose types into something more manageable, as well as giving meaning to nonspecific types. Pair<Long, Int> could be OnlineUserCountAtInstant, for example.

A proper choice of type alias can have a profound influence on how readable your code is.

For example:

However, it is extremely important to keep in mind that type aliases aren’t new types! The compiler always expands them to the type they represent, which can lead to mistakes if used improperly:

Since both firstname and lastname are (type aliased) strings, there’s nothing stopping you from accidentally flipping the two.

If you want to prevent errors like this, the next chapter’s got you covered.

Go back to Operators, jump to the Table of Contents, or continue to Inline (Value) Classes.

Join me in Etnetera
Java
Kotlin
Programming
Object Oriented
Functional Programming
Recommended from ReadMedium