Kotlin logo


A concise multiplatform language developed by JetBrains

Language design

The Great Syntactic Shift

As the first public preview of Kotlin is approaching (it will be announced on Jan 10th, 2012, which is less than a week from now!), we are putting some things in order…

In particular, we have reviewed syntactic forms available in the language, and decided to change a few. These changes are incompatible with the old syntax, and we have migrated all our test data, and will update the publicly available documentation soon.

I would like to point out that this is not the last change of this sort. Kotlin is not released yet, and until it is we are constantly gathering feedback and sometimes find out that something needs to be changed. Consequently, there are no backward-compatibility guarantees before the 1.0. We realize how important backward compatibility is, but we’d better be backward compatible to a really good design created according to the needs of real people.

Here’s an overview of the changes we’ve made.

The Namespace is dead. Long live the Package.

The concept of namespaces evolved into something so close to Java packages, that we decided to rename it. The namespace keyword is replaced by package keyword. Additionally, namespace blocks are no longer supported.

The Arrow loses weight

An arrow is used in function literals and when expressions. Some languages use a “fat arrow” (=>) and some use a “thin arrow” (->). Initially, we used the fat one, but is has some unfortunate interactions with comparisons, like this:

  val higherThanY  = {x => y <= x}

So we decided to switch to a thin arrow:

  val higherThanY  = {x -> y <= x}

More readable function types

In the old syntax we wrote function types as follows:

val f : fun(Int) : String

which is very close to Kotlin’s function declaration syntax, and seems perfectly logical. Unfortunately, as this feature starts interacting with others, things get a lot worse:

fun max(col : Collection<Int>, compare : fun(Int, Int) : Int) : Int 

Have you got lost in colons? Yes, me too…

So we decided to change the function type syntax to the following:

fun max(col : Collection<Int>, compare : (Int, Int) -> Int) : Int

And a little more

Additionally, we introduced optional parentheses in types, changed the tuple syntax to be distinguishable from parenthesized expression lists and made some minor (backward compatible) changes. All this will be reflected in the docs soon. As usual, your feedback is very welcome.

Stay tuned, and don’t miss the announcement next Tuesday!

image description