Last summer, the blog post issue of Mike Gouline “Kotlin the swift of android” let me meditate a while. This is not the first article to mention a new swift-like intiative for android. A new langage for the JVM is not really new in the java world (just look at this wikipedia list). I already seen some stuff with groovy and android (Groovy is the swift for android), but nothing has been seriously followed (Also forget about swift as the swift for android). Let’s try tasting kotlin…
What is remarkable this time in the Jetbrain’s initiative? A clear alignment with the android community to help solve developers pains (jetbrain provides android studio). Good news! But this is not for the green robot fans only: “Statically typed programming language for the JVM, Android and the browser (100% interoperable with Java)”.
What brings Kotlin?
from Andrey Breslav’s words (the language creator):
- static typing: for maintainability of large code bases;
- smooth migration path: for gradually introducing the new language to our existing code bases affecting the rest of the code as little as possible
- tooling quality: we believe that being productive with a language depends largely on the tooling;
- ease of learning and understanding: in our teams, we do not separate “library writers” from “library users”, and we want all our developers to be equally productive with the language they are using.
Production ready & Java friendly
Ok, really seducing and the 1.0 release just came up. It is qualified as “production ready”. Let’s try it with Spring 🙂
A simple domain entity in seconds, thanks to kotlin’s data class feature :
A simple web controller to expose data (inline functions here):
A JPA Repository for the Customer entity:
And the webapp class with the entry point function:
Nothing amazing here … only some new keywords ?! Note that lateinit indicates the kotlin compiler that the given object will be initialized later (after constructor class constructor). It will then ensure null safety everywhere for you. Otherwise you need to handle injection at constructor level, or deal with “?” operator for null safety checks. We can also notice how the old java static functions are used.
It looks like good old java spring webapp, but some smart crunchy stuff 🙂
Here is a repository test:
We can use injections and write tests as a normal junit/spring would do. Note the equality between objects, which directly compares object structure and not references.
And a Controller test with MockMVC:
I’m using Mockito API. Forget about any kind of static imports! It works, but it’s ugly 🙁 This is more verbose than before: we need to explicitly call static methods. Mockito is not kotlin friendly !
A crunchy good taste, but pay attention to hard parts…
This is a simple direct try to use kotlin “as is” with spring boot. We can greatly directly benefits from kotlin’s virtues (data classes, properties, null safety, concise syntax, higher order functions …). Integration with java and spring framework is quite smooth. But we must be carefull with strings which are templated in kotlin (use @Value injection …) and default constructor classes (jackson …). Sebastien sum up some extra hints at the end of his article.
API builder which are using static methods are more verbose … there is no static in kotlin. It’s by consequent less natural to use. The mockito “when” function is quoted because of the “when” keyword, a very smarter substitutes the java switch case. Mockito and MockMVC must be reviewed to offer an easy to use API for Kotlin. A kotlin version of such frameworks would use companion objects to allow such builder API.
The next silver bullet?
Kotlin isn’t a revolution, but a really smart evolution. It gathers good ideas and features that are really missing for java people for quickly writing safe applications. Jetbrains can count on the android community and will easily spread it by intellij and android studio. Existing java community like spring can be easily addressed too. Even if SpringSource has granted groovy as its funky java alternative since some years, Spring is a good target for kotlin playground.
You can already found interesting initiatives with kotlin, such as Anko, a new way to write your layout in android and forget ugly xml.
At ekito, we will dive deeper in Kotlin with a more complete presentation and a coding dojo!
Keep in touch.Google+