Kotlin new version introduces a number of new language features – most notably coroutines – and improved support for its JavaScript target, writes Kotlin marketing manager Roman Belov.

Although still considered experimental, one of the key new features in Kotlin 1.1 are coroutines, which are available through the use of three higher-level constructs: async , await , and yield . This is, for example, how you can use async and await to handle asynchronous operations:

// runs the code in the background thread pool fun asyncOverlay() = async(CommonPool) { // start two async operations val original = asyncLoadImage("original") val overlay = asyncLoadImage("overlay") // and then apply overlay to both results applyOverlay(original.await(), overlay.await()) } // launches new coroutine in UI context launch(UI) { // wait for async overlay to complete val image = asyncOverlay().await() // and then show it in UI showImage(image) }

Note the use of launch , which starts a coroutine. In fact, await can only be used from within a coroutine or from a function that is declared with the suspend keyword, which makes the compiler generate the appropriate code to run that function inside a coroutine:

suspend fun workload(n: Int): Int { delay(1000) return n }

In the example above, delay suspends the coroutine without blocking its associated thread.

Coroutines can also be used to lazily generate sequences using yield :

// inferred type is Sequence<Int> val fibonacci = buildSequence { yield(1) // first Fibonacci number var cur = 1 var next = 1 while (true) { yield(next) // next Fibonacci number val tmp = cur + next cur = next next = tmp } } println(fibonacci.take(10).joinToString())

Being experimental, coroutines are opt-in only and their API may change in future releases.

Other significant new features added to Kotlin 1.1 are:

Type aliases, which allow you to define an alternative name for a type.

The :: operator to get a member reference to a method of specific object.

operator to get a member reference to a method of specific object. Data classes can now be extended.

Destructuring support in lambdas.

As to JavaScript support, the main effort for version 1.1 has been aimed to bring JavaScript support on a par with JVM’s. This means that all language features are available for both targets, with the exception of reflection, currently not available for JavaScript. In particular:

Large parts of Kotlin standard library can now be used on JavaScript.

Generated code is friendlier to JavaScript tools such as minifiers, optimizers, etc.

The external modifier can be used to declare in Kotlin classes that are implemented in JavaScript.

Kotlin can be tried using its online REPL, or installed in a number of different ways.