
Kotlin education: Beyond the basics
Introduction
Kotlin is making steady progress to become the Java replacement for most developers in the world. As far as I know, it’s been widely adopted by companies to develop their main applications. From my point of view and coming from a Java background, it doesn’t have a steep learning curve. However, you still have to learn it to get the most out of it.
When embracing it in your company, it might happen that not every member of the team is on the same page. Closing the gap between different knowledge levels is important.
This article aims to collect some Kotlin topics that go beyond basic knowledge to help you in your Kotlin education.
Sequences
Whenever you are performing some operations on Collection
types, consider converting your Collection
to a Sequence
. You can use the asSequence()
operator to convert it and there are methods available to perform the inverse operation. Sequences support the same API as Collections.
In a Sequence
, the elements are evaluated lazily so you’re not wasting memory creating intermediate Collections as a result of each chained operation.
Elements in Collections are evaluated horizontally whereas Sequences evaluate the elements vertically
What does it mean? Let’s see an example:
listOfGames
.map(Game::name)
.filter { it.startsWith("PS4") }
.take(2)
With a List
:
- All the games you have in the list are going to be processed and a new list is going to be created just with the name of the games.
- From that list that has been created, we’re going to process each element and we’re going to filter out those elements that don’t start with
PS4
. - That has created another new list with just the names that start with
PS4
. From that list, we’re going to return a new list with the first 2 elements.
As you can see, we’ve wasted so much memory and CPU trying to compute all the elements when at the end of the day, we just wanted two of them.
With a Sequence
, for each element:
- We get the name (that is going to be stored temporarily).
- If the element starts with
PS4
, it remains in the Sequence. - Repeat until we have 2 elements in the Sequence.
As you can see, it’s more efficient and it’s perfect when you don’t know the size of the list or it can be potentially big. How can you convert the code before to work with Sequences and have the same output?
listOfGames.asSequence()
.map(Game::name)
.filter { it.startsWith("PS4") }
.take(2)
.toList()
Infix notation in functions
Infix notation is a meta-language feature that allows a more readable code and brings you closer to natural language sentences. It’s often used in Kotlin libraries and DSLs to hide simple logic and replace it for human-readable keywords. Be careful when using them, if misused, they can lead to chaos and make your code difficult to understand.
Article by Arturo Gutiérrez:
Sealed classes
Good replacement for Enum classes. Sealed classes gives you even more benefits when used in when
expressions as an assignment or a return: it’s going to error out if you don’t consider all the types of the sealed class.
Article by Philipe Steiff
Kotlin — What is a Sealed Class?
Let’s start with a simple definition about sealed classes:
medium.com
Objects
Remember that every time you see the keyword object
, the compiler is going to create a new class.
Kotlin hidden costs
Must-read articles by Christophe Beyls
Delegated Properties
Generics: in, out, where
Annotations
Article by Tomek Polański
Reflection
DSLs
Kotlin in Action is a great book, you should read it. It covers all the basics of Kotlin but also gives you some deep understanding of some topics that are going to help you understand why some Kotlin features are like that.
Some sections are free in their website. Fortunately, the section about DSLs is. Check it out!
Another article about how to create a DSL in Kotlin
Thanks for reading,
Manuel Vicente Vivo