days
1
3
hours
2
2
minutes
1
2
seconds
1
1
search
Java 9 & Java EE 8: They are finally here!
It's here, it's here

Kotlin 1.1 brings JavaScript support and coroutines

Gabriela Motroc
Kotlin 1.1

Welcome neon sign image via Shutterstock

It’s been less than two months since Kotlin 1.1 reached beta but now it’s here — Kotlin 1.1 is “a big step forward enabling the use of Kotlin in many new scenarios.” Let’s find out why.

Kotlin 1.1 is here. JetBrains’ Roman Belov said in the blog post announcing the release that Kotlin 1.1 is  “a big step forward enabling the use of Kotlin in many new scenarios.” The most important changes are the JavaScript target — which is no longer experimental— and the support for coroutines.

Kotlin 1.1 includes type aliases, bound callable references, destructuring in lambdas and more. But first, let’s focus on the most exciting features.

JavaScript target —  experimental no more

The JavaScript target is no longer considered experimental. This means that 1.1 supports all language features, a large part of the standard library and JavaScript interoperability. This change allows users to migrate the browser frontend of their applications to Kotlin while continuing to use modern JavaScript development frameworks such as React.

Kotlin for JavaScript has dynamic types to interoperate with “native” JavaScript code. Use the ts2kt converter together with headers from DefinitelyTyped if you want to use well-known libraries through typed APIs. Furthermore, both Node.js and the browser are supported. The Kotlin Standard Library is available for use through npm.

Check out the docs if you’d like to find out how to use it.

 SEE ALSO: What Kotlin is all about — As explained by Hadi Hariri

Support for coroutines

The support for coroutines is the star of this new release — coroutines bring the support of async/await, yield and similar programming patterns. The best part is that the implementation of coroutine execution is part of the libraries, not the language, which means users aren’t bound to any specific programming paradigm or concurrency library.

A coroutine is pretty much a light-weight thread that can be suspended and resumed at a later time. They are supported through suspending functions: a call to such a function can potentially suspend a coroutine. To start a new coroutine, JetBrains uses an anonymous suspending functions (i.e. suspending lambdas).

Let’s have a look at async/await which is implemented in an external library, kotlinx.coroutines:

// 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)
}

async { ... } starts a coroutine and when they use await() the execution of the coroutine is suspended while the operation being awaited is executed. It is then resumed (possibly on a different thread) when the operation being awaited completes.

Keep in mind that they are considered an experimental feature, so the team is not committing to supporting the backwards compatibility of this feature after the final 1.1 release. Know that the coroutine-related API in the Standard Library is put under the kotlin.coroutines.experimental package. When the design is finalized and the experimental status lifted, the final API will be moved to kotlin.coroutines, and the experimental package will be kept around (probably in a separate artifact) for backward compatibility.

For more information about coroutines, check out the docs.

JVM backend

Kotlin has now the option to generate Java 8 bytecode (-jvm-target 1.8 command line option or the corresponding options in Ant/Maven/Gradle). Although this does not change the semantics of the bytecode, the team plans to make further use of this later.

Furthermore, there are currently separate versions of the standard library supporting the new JDK APIs added in Java 7 and 8. If you need access to the new APIs, use kotlin-stdlib-jre7 and kotlin-stdlib-jre8 maven artifacts instead of the standard kotlin-stdlib. Keep in mind that these artifacts are tiny extensions on top of kotlin-stdlib and they bring it to your project as a transitive dependency.

Last but not least, to prepare for Java 9 support, the extension functions and properties in the kotlin-reflect.jar library have been moved to the package kotlin.reflect.full. The names in the old package (kotlin.reflect) are deprecated and will be removed in Kotlin 1.2. It is important to keep in mind that the core reflection interfaces (such as KClass) are part of the Kotlin standard library, not kotlin-reflect, and are not affected by the move.

Find out more about Kotlin 1.1 here.

Author
Gabriela Motroc
Gabriela Motroc is an online editor for JAXenter.com. Before working at S&S Media she studied International Communication Management at The Hague University of Applied Sciences.

Comments
comments powered by Disqus