Kotlin M5.1

Posted on by

There have been enough little improvements since Kotlin M5 so that we are rolling them out as M5.1 today. Some of them are not so little in fact, like enabling the use of Scala libraries, such as Akka. This post gives a quick overview of these changes.

Better Support for Scala Classes. Akka

Theoretically, it is very easy for all JVM languages to interoperate. In practice, there appear to be numerous little problems that make it unpleasant or practically impossible.

One of such problems was related to the ambiguous use of “$” signs in class names, which is a well-known issue on the JVM. It being fixed now, allows you to use some Akka classes you couldn’t use before, such as Duration.

To get an impression of what Akka looks like in Kotlin, have a look at this example.

Even More Helpful IDE

With the help of students of Cornell and Jagiellonian universities, we got quite a few quick fixes implemented in M5.1. When the IDE complains about some error or warns you, you can simply press Alt+Enter and get a list of proposed fixes:

Parameters are Immutable

We removed support for mutable parameters, as in

fun foo(var x: Int) { x = 5 }

The main reason is that this was confusing: people tend to think that this means passing a parameter by reference, which we do not support (it is costly at runtime). Another source of confusion is primary constructors: “val” or “var” in a constructor declaration means something different from the same thing if a function declarations (namely, it creates a property). Also, we all know that mutating parameters is no good style, so writing “val” or “var” infront of a parameter in a function, catch block of for-loop is no longer allowed.

If some of your existing code breaks, you can quick-fix the whole project in one click using the IDE:

http://www.youtube.com/watch?v=JY-Vx8FjtIM

Support for Java’s protected static methods

Some Java frameworks, like Android, rely on protected static methods to be available in subclasses. Although this seems like a questionable pattern, Kotlin now supports it (only for Java compatibility), i.e. you can access a protected static member of a Java class if you extend this class in Kotlin.

Anonymous Objects

Consider the following code (that uses a Kotlin analog to anonymous inner class):

val x = object : A() { ... }

What is the type of x? It used to be the anonymous type, but if you use x from the outside, you can not access it: the type is not valid. Now the type will be A. This applies only to properties that can be seen from the outside, i.e. if x is a local variable it will still have the anonymous type, since it is harmless.

Class Objects are Usable from Java

Kotlin classes do not have static members, but rather have class objects:

class A { class object { val x = 1 } }

Now, with a few bugs fixed, you can access members of class objects from your Java code:

public static void main(String[] args) { System.out.println(A.object.instance$.getX()); }

Compiler

The compiler is being improved too: a few fixes for corner cases of nullable types interacting with generics and optimizations for loops over ranges.

Requirements

Kotlin M5.1 requires IntelliJ IDEA 12.0.4 (EAPs of 12.1 are not supported yet), you can download it from the plugin repository.

Have a nice Kotlin!