Like Groovy and Scala before it, Kotlin is a new programming language for development on the JVM. After reading that Kotlin was being developed by Jetbrains, the folks behind the incredible Java IDE IntelliJ, I decided to give it a try a few weeks ago. Here are some of my initial impressions of the language.

Kotlin claims to be very simple for Java programmers to learn, and I agree as I was able to pick up the langauge easily and quickly convert some existing Java and Groovy code to Kotlin.

One piece of code I had was originally written in JavaScript which I then converted to Groovy and then to Kotlin. In both JavaScript and Groovy this piecewise variable was dynamically typed, however with Kotlin being strongly type I obviously have to type it.

    var piecewise: ((List<Double>,
                     (Double, Double) -> (Double) -> Double,
                     (Double, Double) -> (Double) -> Double) -> (Double) -> Double)? = null

Wow! I’m not sure readabiity has been enhanced at all but typing this (though type safety certainly has been). Kotlin 1.1 is coming soon and with it the introduction of typealias which will allow a type to be mapped to an alias, which could help in this situation.

The Good

The Bad

Method parameters are val/final

In Kotlin, method parameters are implicitly val (final in Java-speak). I know that some people consider changing the value of a method parameter to be poor practice - and I’ll admit that I do not do it often - but at times it can be very convinent. I wouldn’t mind if method parameters were val by default but could overriden to var, however Kotlin doesn’t allow this. From what I’ve read it looks like you used to be able to make make method parameters var in earlier version of Kotlin but this functionality was removed. Bummer!

Classes are sealed/final

In Kotlin, all classes are sealed (final in Java-speak). This enforces a recommendation made in Effective Java (that all classes should be final unless intended for modification). I don’t think this should be enforced at the language level. I can getting frustrated trying to extend Kotlin code written by others and being prevented from doing so because the class wasn’t opened. I know so far, I ALWAYS forget to open a class until I go to subclass it.

No traditional for loops

That’s right, they killed the traditional for loop (initialization, loop-continuation condition, increment), an only have a for-each style for loop.

For example, in Java: java int x = 0; for(int i = 0; i < array.size(); ++i) { if(i % 2 == 0) x += 0; else x -= 0; }

Can’t be directly translated to Java. It isn’t hard to replicate

The Ugly

IDE Issues

I’ve been using Apache Spark (written in Scala) in some Kotlin code recently and it has caused some IDE issues. Slow performance in the editor, deadlocking IntelliJ and in some cases causing the dreaded KotlinFrontEndException. The following code is an example code snippet I wrote which crashes the Kotlin compiler:

import org.apache.spark.sql.functions.*
import org.apache.spark.sql.types.DataTypes
import org.apache.spark.sql.SparkSession

fun main(args: Array<String>) {
	val sparkSession = SparkSession.builder()

    val func = { a: Double, b: Double ->
    	 a + b

    val udf = udf(func, DataTypes.DoubleType)
    sparkSession.udf().register("foo", udf)

I’ve subitted a bug report to JetBrains and they’ve flagged it as a Major priority so hopefully the problem will be resolved soon. It was a bit dissappointing to hit such a show-stopping issue so early in my prototyping with Kotlin but thanks to it’s excellent Java interopability I’m able to work around it by keeping my code which causes this issue in Java for now.