Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News JetBrains Debuts New Kotlin Compiler K2, Kotlin for WebAssembly, and More

JetBrains Debuts New Kotlin Compiler K2, Kotlin for WebAssembly, and More

This item in japanese

Lire ce contenu en français

At its recent Kotlin 2021 Premier event, JetBrains made a number of major announcements, including K2, the new, faster Kotlin compiler, support for WebAssembly, the Kotlin Symbol Processor, a new code coverage plugin, and improved static analysis.

Named after what is now considered the highest mountain in the world, K2 aims to provide a new foundation for Kotlin's future evolution and a common basis for all supported platforms. Additionally, it is faster and brings a new API to create compiler extensions.

Possibly the most compelling change brought by K2 is the adoption of new IR-based backends, whereas the old compiler generated target code directly from the syntax tree and semantic info without using an intermediate representation. This is a far reaching move that allows K2 to share some logic across its different backends, namely the JVM, JS, and native backends. For example, all optimizations can now be applied at the IR-level, which is common for all target platforms. Likewise, the process of adding new features to the language and supporting them on all target platforms will also benefit from this.

K2 does not only include new backends for supported platforms, it also provides a new frontend, which is mostly aimed to improve performance and better support for advanced IDE features, such as syntax coloring, syntax checking, and so on. The new frontend is built around a frontend intermediate representation (FIR) which is optimized for call resolution and takes care of desugaring a number of language constructs into simplified representations. For example, +, += and other operators are desugared into explicit function calls, for is desugared into a while when using an iterator, etc.

As mentioned K2, is significantly faster then the old compiler. According to JetBrains benchmarks, you could expect a 2x speed-up.

The integration between Kotlin and the Web space is getting a new player with Kotlin/Wasm.

Kotlin for WebAssembly will provide users with a powerful tool for writing code that is optimized for load-time efficiency, size, and predictable speeds. It will allow you to use your favorite language – Kotlin – and its familiar concepts to write code that can interact with JavaScript applications.

While Kotlin/JS remains the technology to go to integrate Kotlin in a Web app, Kotlin/Wasm aims to provide a new tool that will fit a more specific scenario where you want to carry through some computationally expensive tasks.

Kover is Kotlin's new plugin for code coverage, which can be used for code built with the Kotlin/JVM compiler. The tool can be used as a Gradle task, locally on a developer's machine, or integrated within a CI pipeline. It also works with Kotlin Multiplatform and is compatible with Java code coverage library JaCoCo.

If you are interested in metaprogramming, you will surely appreciate the work JetBrains and Google have been doing around Kotlin Symbol Processing (KSP). JetBrains says KSP can be seen as a pre-processor of Kotlin programs, only it actually understands them. This means KSP explicitly represents constructs like classes, class members, functions, and associated parameters and makes them accessible to processors in a structured way. However, KSP does not allow processors to modify the code of a program, which is treated as read-only. Kotlin KSP is now stable and provides the basis for Room and Moshi, two annotation processors, and several other plugins.

As a final note, most Kotlin programmers will welcome Qodana for Kotlin, a static analyzer for Kotlin programs. Qodana, which also supports additional languages, can be run as a standalone tool or integrated within you CI pipeline. In addition, it can be integrated with GitHub Actions and can use third-party linters.

Rate this Article