Programming language: Kotlin 1.3 stabilizes the coroutines

In addition, Kotlin expands the handling of multiplatform projects and introduces so-called contracts.

 

JetBrains has released Kotlin 1.3 after seven minor versions of its predecessor 1.2.x. The main changes concern above all the coroutines introduced in Kotlin 1.1, which are now considered stable, and the multiplatform projects introduced in version 1.2. There are also some linguistic additions and a first beta of Ktor 1.0.

Coroutines help in writing concurrent programs. The concept, which is also familiar in other languages , was introduced by Kotlin in version 1.1 . Since they were previously considered experimental, they are marked stable in Kotlin 1.3. The corresponding library kotlinx.coroutines now has the version number 1.0.

Coroutines make it possible to write non-blocking asynchronous code. In simple terms, the routines may interrupt their flow, for example, waiting for the transmission of a file over the network without blocking the active thread.

Support for creating cross-platform projects came in version 1.2 of the programming language. Although it is still considered experimental in version 1.3, it has received some additions. So the current release has libraries for typical task fields on board, including an HTTP client and a serialization library . The kotlinx.coroutines library for coroutines is also designed for multiplatform projects .

Multiplatform projects are very important for the programming language, which evolves from its roots as a pure JVM language (Java Virtual Machine) to a broad application. Developers can compile code for JavaScript as well as native binaries. The latter helps, among other things, in creating mobile cross-platform projects that run on both Android in the JVM and on iOS with Kotlin / Native.

Kotlin relies on a combination of cross-platform code specific to each target platform. In the first instance, internal expect fun can be used to define a kind of wrapper for functions that define the platform-specific implementations as internal actual fun , for example to implement the response of GUI elements to the respective target platform.

Kotlin 1.3 introduces the concept of so-called Contracts, with which functions can tell the compiler how they behave. The following example from the Kotlin documentation states with contract that when the request is executed successfully, the Boolean variable condition is true . Thus, in the foo function shown below, the compiler can make a smartcast on s because the contract determines that the condition is met on successful completion and otherwise an exception would occur:

[cc lang=”javascript”]fun require (condition: Boolean) {
// This is a syntax form, which tells compiler:
// if this function returns successfully, then passed ‘condition’ is true
contract {returns () implies condition}
if (! condition) throw IllegalArgumentException (…)
}

fun foo (s: String?) {
require (s is string)
// s is smartcasted to ‘string’ here, otherwise
// ‘require’ would have throw an exception
} [/cc]

Contracts are generally still considered experimental, but have already found their way into the standard library and are marked as stable insofar as developers do not have to explicitly activate them.

Other notable linguistic additions include inline classes that are considered experimental. These are limited versions of traditional classes that are marked with the keyword inline and must have exactly one property. The compiler uses this restriction for optimizations, for example by avoiding the constructor call at appropriate places and immediately setting the property.

Also new are the four variable types kotlin.UByte , kotlin.UShort , kotlin.UInt , kotlin.ULong for unsigned values ​​with 8, 16, 32 or 64 bits. Above all, they should enable low-level functions such as byte manipulation. They too are currently considered experimental.

It’s not surprising that Kotlin 1.3 brings connections for the new features to IntelliJ in both the Community edition and the Ultimate version, because the tools as well as Kotlin come from JetBrains. The IntelliJ IDEA-based Android Studio gets the full tool support. Among other things, the development environments offer autocompletion and refactoring for all new features in Kotlin 1.3, for the creation of multiplatform projects and also for Kotlin / Native. The latter has been officially in beta since the KotlinConf in early October .

The Ktor framework for creating asynchronous, connected applications on the client and server side, such as web applications or HTTP services, relies on coroutines for non-blocking operations. Ktor 1.0 has now reached beta status. Currently, however, only JVM applications can be created. JavaScript clients and servers or clients in Kotlin / Native are planned.

More details about Kotlin 1.3 can be found in the Kotlin blog . The current command-line compiler is available in the GitHub repository . In the development environment, IntelliJ IDEA 2018.3, which will probably be released within the next week, already has Kotlin 1.3 on board. In Eclipse, the latest release can be installed from the Marketplace , and in Android Studio, it is available through the Plug-in Manager. Instructions for installing the latest Kotlin version via Maven / Gradle can be found in the Kotlin documentation .

 

Source: www.heise.de

 

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here