BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin 1.2 Introduces Multi-Platform Projects

Kotlin 1.2 Introduces Multi-Platform Projects

Leia em Português

This item in japanese

Bookmarks

The latest version of Kotlin makes it possible to share code for the JVM and the JavaScript platform using multi-platform projects. Additionally, it includes a number of language and library improvements, and better compiler performance.

After introducing support for the JavaScript target in Kotlin 1.1, Kotlin now brings developers the possibility to easily manage projects meant to run both on the JVM and the JavaScript platforms. In future, support for native binary will also be included thanks to Kotlin Native.

The basic idea with multi-platform projects is organizing your modules into a set of common and platform-specific modules. Platform-specific modules contain code that either runs on the JVM or on JavaScript and can freely access platform-specific libraries. For each platform-specific module, a common module shall exist that provides so called “expected declarations”, i.e., declarations that must be implemented in the platform-specific module. Thanks to this mechanism, you can call platform-specific code from platform-independent code. While common modules can only contain Kotlin code and use Kotlin libraries, platform modules can also contain code in either Java or JavaScript and are compiled into target-specific format.

As an example of this here, is how you can define a common module and its corresponding platform module:

// Common module

package org.jetbrains.foo

expect class Foo(bar: String) {
    fun frob()
}

fun main(args: Array<String>) {
    Foo("Hello").frob()
}

// JVM module

package org.jetbrains.foo

actual class Foo actual constructor(val bar: String) {
    actual fun frob() {
        println("Frobbing the $bar")
    }
}

An important development aiming to make Kotlin more capable of expressing behaviour in common modules is a set of cross-platform libraries, including a serialization library, a testing library, and an HTML isomorphic library. This should reduce the necessity of resorting to platform modules.

On the language front, Kotlin 1.2 provides improved smart casts and type inference in certain cases; a new kotlin.math package in the standard library; and new standard library functions to work with sequences and collections.

As a last note, Kotlin’s compiler has become significantly faster in version 1.2, outperforming Kotlin 1.1’s by 25%. Further improvement is planned for future minor releases of 1.2.

You can get the full details of what’s new in Kotlin 1.2, which is already bundled in IntelliJ IDEA 2017.3, in the official release notes.

Rate this Article

Adoption
Style

BT