Exploring Kotlin
I’ve been exploring Kotlin recently. Kotlin is a new statically typed JVM language and was announced about half a year ago. This month JetBrains released its first milestone candidate which I decided to have a closer look at.
First of Kotlin is still in its early stages. A lot of things work pretty well (I’m especially impressed by the IDE support) but there are still numerous issues. I’ve hit a couple of bugs so far.
Features
I’ll highlight a few language features which I think are interesting (coming from Java).
Null-safety
fun demoVars() {
val first : String = "abc"
first = "xyz" // error, variable is immutable
var second = "abc"
second = null // error, variable is mutable but non-nullable
var third : String? = "abc"
third = null // ok, variable is marked as nullable ('?')
foo?.bar?.baz // safe navigation
}
Multi-line strings and string templates
fun demoMultiLineTemplate() {
val first = "john"
val second = "doe"
println("""
First name: $first
Second name: $second
""")
}
Higher-order functions
bar({ println("I'm foobar") })
bar({ println("I'm foobar") }, { println("I'm foobaz") })
fun bar(foobar : () -> T) {
foobar()
}
fun bar(foobar : () -> T, foobaz : () -> B) {
foobar()
foobaz()
}
These a just some examples refer to the Kotlin docs for more features.
Next blog
In my next blog I’ll show a larger example of Kotlin code and compare it with its Java equivalent.