BT

Your opinion matters! Please fill in the InfoQ Survey!

Kotlin 1.2 Introduces Multi-Platform Projects

| by Sergio De Simone Follow 6 Followers on Nov 30, 2017. Estimated reading time: 1 minute |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT