BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Kotlin 1.4 Brings New Language Features, Improved Compilers and Tools

Kotlin 1.4 Brings New Language Features, Improved Compilers and Tools

This item in japanese

Kotlin 1.4 focuses on improving performance and tooling. It also contains a number of new language features, including single abstract method (SAM) conversion for interfaces, explicit API mode, and more.

Our main focus for this release was improving the overall development experience with Kotlin, including improved IDE performance and stability. Our goal is to make Kotlin users as productive as possible, and we focused on improving the things that are most important to users, so they feel even happier when using it!

Kotlin plugin for IntelliJ IDEA 2020.1+ and Android Studio 4.1+ fixes more than 60 performance issues. Thanks to this, for example, syntax highlighting is now 1.5-4 times faster, says JetBrains. Similarly, autocomplete suggestions take less time to appear, with 95.4% of cases requiring less than 500ms, up from 92.4% in the previous release, and only 1.3% of cases requiring more than 1s.

JetBrains has also released parts of the new Kotlin compiler front-end, which aims to be much faster and unify all supported platforms. In particular, the latest compiler uses a more powerful type inference engine and includes new, alpha-quality JVM and JS backends.

The new type inference engine extends the number of cases where types can be inferred, supports smart cast for lambdas's last expression and callable references, and more. Kotlin/JVM and Kotlin/JS have been migrated to use the same IR as Kotlin/Native, which enables implementing most features, optimizations, and bug fixes only once for all platforms.

Speaking of language features, Kotlin 1.4 contains many new features, including single abstract method (SAM) conversion for interfaces, explicit API mode for libraries, support for mixing named and positional arguments, trailing comma, and others.

Single abstract method conversion is a feature that existed in previous Kotlin releases but could only be used with Java methods and interfaces. Now, it can be also used for Kotlin interfaces. In short, single abstract method conversion enables passing a lambda as an argument when an interface with just one single abstract method is expected:

fun interface IntPredicate {
    fun accept(i: Int): Boolean
}

val isEven = IntPredicate { it % 2 == 0 }

fun main() { 
    println("Is 7 even? - ${isEven.accept(7)}")
}

When using explicit API mode, Kotlin compiler enforces two specific rules meant to improve library interfaces: if default visibility is public, visibility modifiers are required for all declarations to prevent any of them from being exposed unintentionally; similarly, explicit type specifications are required with properties and functions belonging to the public API.

Other convenient language features Kotlin 1.4 includes are support for mixing named and positional arguments and for trailing comma. In Kotlin 1.4 you can specify a named argument in the middle of positional arguments and you can mix named and positional arguments freely, provided they remain in the correct order. Trailing comma is allowed in argument and parameter lists, in when entries, and in destructuring declarations.

On the tooling front, worth mentioning are a new project wizard and a coroutine debugger which allows you to check the status of each coroutine, inspect values of local and captured variables, and inspect the call stack leading to the coroutine call as well as the frames belonging to the coroutine itself.

Kotlin 1.4 is in all respect a major version including a lot of new stuff which cannot be covered entirely here. Do not miss the official release notes for the full detail.

Rate this Article

Adoption
Style

BT