Kotlin

4th of October 2015

Some weeks ago, at the Droidcon 2015 in Madrid, several speakers like Antonio Leiva, talked about a programming language that called my attention, Kotlin. I had already heard about Kotlin but not in so much detail, so I started to research and do some demos, the result was good: I really liked it. Its learning curve is surprisingly easy and fast and its functional components are really interesting.

Disclamer: I am a kotlin beginner so any suggestion will be welcome. So far, I only have done a few introduction exercices that Jetbrains people call Koans, bellow you can find the link to the official web page and also to my github repo with the first block solved, I hope you find them useful:

Kotlin is created and maintained by Jetbrains, the company behind IntelliJ, and it runs on the JVM (Java Virtual Machine). It is an object oriented language and as I mentioned before, it has some functional aspects which make it a very interesting Java alternative.

Why I like Kotlin

Installation

Very simple, Jetbrain people have done really nice. Requirements:

  1. IntelliJ 14.1 or newer, it also works with Andorid Studio: IntelliJ IDEA Minimal Survival Guide. You can download it from downloads from Jetbrains.
  2. JDK 1.6 or superior: You can download it from the official web page Oracle, or maybe you prefer to use this fantastic tutorial JVM Minimal Survive Guide
  3. Kotlin plugin: You can download it from the IDE, just open IntelliJ and go: File -> Plugins and search ==Kotlin==.
  4. Done :).

Compiler

Kotlin called my attention for its compiler, so far I didn’t know anything about JVM based programming languages so it was very interesting what I found . To compile mixed projects, in our case Kotlin, requires it to understand the Java source files and its binaries, it also requires Java to understand the Kotlin source files and it binaries. The idea of Kotlin compiler to understand Java source files is easy to imagine, but the opposite thing, to make the javac (Java compiler) to understand Kotlin files is hard.

So far the state of the art is the following.

Characteristics:

Java interoperabilty

Kotlin was designed with interoperability in mind, and they achieved it! I didn’t try it too much, just some test code doing the koans and seems really easy to understand and to implement in both sides, from Kotlin and from Java.

Here the documentation is very clear so I recommend taking a look to it.

More information on the Official page or you can check the resolved koan in Github

Null Safety

This is one of my favorite characteristics that solves one of the most disgusting problem when programming, the null reference. Null in Kotlin is a “first-class citizen” in its type system, in other words, types are aware that they can be null. In my opinion this is very useful since the NullPointerException in Java is something really nasty and evolves into a lot of boilerplate code and defensive programming.

Kotlin can make distinction between those types that can make a reference to null and those that cannot.

var a: String = "abc"
a = null // compilation error
val l = a.lenght() // this will no generate a NullPointerException

We can make a type “nullable”

var b: String? = "abc"
b = null // ok
val l = b.length() // Now this can generate a NullPointerException so it will no compile.

But if you really want to deal with null, Kotlin gives us different ways to check any possible null reference:

More information on the Official page or you can check the resolved koan in my Github

High order function and Lambdas support

I love it. Basically a high order function is a function that can take other functions as parameters o it can return another function, here you got and example

list.filter {it % 2 == 1}

In the example a list is filtered removing all the even elements, it uses a predefined function filter which you can pass any other function as an argument it % 2 == 1 and will be applied to the whole collection.

Probably the easiest way to understand it reading the Official documentationand checking the resolved koan Github

Data Classes

Very useful, data classes are those which only exist to store information, Data Classes will generate certain functionalities such as equals(), hasCode(), toString or copy function, Currently Google offers the AutoValue library wich do the same AutoValue.

data class Money(val currency: String, val amount: Int)
val money = Money("USD", 100)
val moreMoney = money.copy(amount = 200)

We can declare default values, so we don’t need to declare different constructors.

data class Money(val currency: String = "Euro", val amount: Int = 0)

And we have Multi-declarations:

val jane = User("Jane", 35) 
val (name, age) = jane
println("$name, $age years of age") // Imprime "Jane, 35 years of age"

More information on the Official page or you can check the resolved koan in Github

Traits

You can understand Traits as interfaces where you can implement functions, I have no example since I only know them from Scala and since then I really miss them in my daily work, you have got more info in this post.

Extension Fuctions

Kotlin allows to extend the functionalities of any class (Kotlin or Java) with no heritage, just using the “extension” declaration, this members are introduced in a static way and make us to forget the “Utils” classes full of static functions, this way we obtain a cleaner code.

fun String.last() : Char {
return this[length - 1]
}
val x = "Hey!"
println(x.last()) // Prints "!".

Reading about this I discovered that other languages like C# also implements this functionality.

More information on the Official page or you can check the resolved koan in Github

Conclusion

To be honest, as I mentioned before, my adventure with Kotlin has been short, I only made some programming doing the koans but I a really like it and I think it gives new opportunities to Java. I hope you like the post and thank for reading it!

References:


Published on the 4th of October 2015 by Alfredo Cerezo.
Kotlin