days
-6
-8
hours
-1
-1
minutes
-5
-6
seconds
-1
-5
search
Kotlin 1.3-M1 is here

A first look at Kotlin 1.3: Coroutines no longer experimental

Gabriela Motroc
Kotlin 1.3
© Shutterstock /  Dooder

We’ve talked about the 1.2.X updates, now it’s time to have a look at what the future holds for Kotlin 1.3. The first preview version of the new major release is here.

Kotlin 1.2.50 was released last month and while we’re keeping an eye on the 1.2.X updates, it’s time to move on to the next big thing: Kotlin 1.3.

The first preview version is here and it arrives with lots of advancements in tow, including graduation of coroutines, new experimental unsigned arithmetic, and much more.

As usual, you’ll find the complete list of changes in the changelog.

What’s coming in Kotlin 1.3

Coroutines no longer experimental

As mentioned above, there are a lot of great advancements but the most interesting must be this one right here: coroutines will not be experimental anymore in 1.3, as JetBrains’ Ilya Gorbunov wrote in the blog post announcing Kotlin 1.3-M1.

Pop the champagne, coroutines will not be experimental anymore in 1.3. Both the syntax and the Standard Library APIs are stabilized and will remain backwards-compatible in the future. Let’s take a quick trip down memory lane: coroutines were introduced in Kotlin 1.1, which debuted earlier this year.

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).

But enough about how they were, let’s focus on how they changed. Some notable features include:

  • KT-16908 Support callable references to suspending functions
  • KT-18559 Serializability of all coroutine-related classes

Speaking of stuff that’s no longer experimental, you should know that the core APIs have been simplified and moved out of the experimental packages. The team is also actively working on a multiplatform version of the coroutine APIs, including iOS support through Kotlin/Native.

Let’s recap: All the packages with coroutine-related functions in Kotlin have dropped experimental from their package names, and buildSequence and buildIterator functions have moved to their permanent place in the kotlin.sequences package. However, on the language level, there is still a suspend modifier to support coroutines, and all the rules for working with coroutines remain mostly the same as they were in the experimental version, Gorbunov explained.

The graduated Continuation<T> interface has been simplified: there’s only one resumeWith(result: SuccessOrFailure<T>) member function.

The resume(value: T) and resumeWithException(exception: Throwable) are now defined as extensions. Don’t worry, though: only a small minority of code that defines its own coroutine builders is affected; classic examples of wrapping callbacks into suspending functions have remained mostly unchanged.

You should keep in mind that graduated coroutines use a different binary interface and are not binary-compatible with experimental coroutines. To make sure the migration is as smooth as possible, the team plans to add a compatibility layer in 1.3 and the classes for experimental coroutines will remain in the standard library, Gorbunov added. Furthermore, the compiled Kotlin/JVM code that uses experimental coroutines from 1.1 or 1.2 would still continue to work in Kotlin 1.3.

Now comes the not-so-pleasant part: the Kotlin 1.3-M1 release does not provide any support for invoking experimental suspending functions from code that is compiled with the language version 1.3. If you still want to try graduated coroutines in Kotlin 1.3-M1 with libraries that expose suspending functions in their APIs, you must use versions of these libraries that are compiled for Kotlin 1.3. Don’t worry, though: this will be fixed in no time.

The team will provide a version of kotlinx.coroutines library with x.x.x-eap13 version that is built with Kotlin 1.3 and drops experimental from its package names for every x.x.x version that we release while Kotlin 1.3 is in the preview phase.

Although the IDE will help you make the migration to new coroutines as smooth as possible, the range of supported migration scenarios will be improved before 1.3 so help is on the way.

SEE ALSO: Kotlin and Java go well together, report shows

New features

Coroutines will finally ditch their experimental status but a bunch of other new capabilities will take their place. Let’s not talk about what’s not ready to be unveiled though; let’s focus on what is ready:

  • Capturing when subject in a variable. The common case of capturing the subject expression of when in a variable is now supported in the language. In short, this makes the response variable properly scoped within when, and makes the logic flow in a more expression-like fashion. Oh and it was one of the most popular feature requests in our issue tracker so your wish has been granted.
  • @JvmStatic and @JvmField in companions of interfaces. Your Kotlin interfaces can now expose static members (fields and methods) to Java clients. However, to use @JvmField on interface companion properties, all of them must be public final vals and all of them must be annotated. Also, the -jvm-target compiler option must be at least 1.8.
  • Nested declarations in annotation classes. Since @JvmField and @JvmStatic are supported in interfaces, you can now declare such members in annotation companion objects.
  • Functional types of higher arity. As you might know already, a function type can now have more than 22 parameters but the team has pushed that limit to 255 parameters, which is the practical maximum number of parameters a method can have on the JVM. To find out how they did it without introducing another 233 classes, see the corresponding KEEP.

Experimental language features

There are three new features in Kotlin 1.3 that will remain experimental for now. Keep in mind that if you want to use them, you’ll need an explicit opt-in, otherwise usages of a feature will be marked as warnings or errors.

Learn more about these experimental language features here.

We’ve only scratched the surface so if you want to see all the goodies in Kotlin 1.3, including the new APIs that are available in the standard library and the breaking changes, read Ilya Gorbunov’s blog post.

Get started with Kotlin 1.3

In Maven/Gradle: Add http://dl.bintray.com/kotlin/kotlin-eap as a repository for the build script and your projects; use 1.3-M1 as the version number for the compiler plugin and the standard library.

In IntelliJ IDEA: Go to Tools → Kotlin → Configure Kotlin Plugin Updates, then select “Early Access Preview 1.3” in the Update channel drop-down list, and then click Check for updates.

The command-line compiler can be downloaded from the Github release page.

On try.kotlinlang.org: Use the drop-down list in the bottom right-hand corner to change the compiler version to 1.3‑M1.

asap

Author
Gabriela Motroc
Gabriela Motroc is editor of JAXenter.com and JAX Magazine. Before working at Software & Support Media Group, she studied International Communication Management at the Hague University of Applied Sciences.