4

Kotlin 1.5 Gets Support for Java 15 Features and a New JVM Compiler

 3 years ago
source link: https://www.infoq.com/news/2021/05/kotlin-1-5-released/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Kotlin 1.5 Gets Support for Java 15 Features and a New JVM Compiler

May 06, 2021 1 min read

Kotlin 1.5 introduces support for the latest Java features, including record classes, sealed interfaces, and inline classes. Additionally, it brings a number of improvements to the standard library and a new JVM IR compiler.

Java records are classes that contain immutable data. A Kotlin data class can be used as a Java record by annotating it with @JvmRecord:

@JvmRecord
data class User(val name: String, val age: Int)

To be used as a record, a Kotlin data class must not derive from other classes, since all Java records derive from java.lang.Record, but can implement interfaces. Additional requirements forbid declaring mutable properties with backing fields and local classes.

Kotlin 1.5 also introduces support for sealed interfaces and relax requirements for sealed classes. Both sealed interfaces and classes are Java features aiming to provide fine-grained control on inheritance by enabling classes and interfaces to specify their allowed subtypes. Allowed subclasses of a sealed class are known at compile time and cannot be added thereafter. Sealed classes can be declared by prefixing them with the sealed modifier. In Kotlin 1.5, sealed interfaces and classes are not required anymore to belong to the same file and can appear in any files in the same compilation unit and package.

Finally, Kotlin 1.5 stabilizes inline classes, which are value types that do not incur memory allocation-related overhead. An inline class can be declared using the value modifier:

@JvmInline //required for the JVM backend
value class Password(val s: String)

As mentioned, Kotlin 1.5 also brings a number of improvements to the standard library. In particular, it introduces a new API for unsigned integers, idiomatic extension functions to use non-blocking Java I/O, and improvements to the String and Char API.

As part of a project to rewrite the whole compiler, Kotlin introduced last year a new IR backend which has become stable and the default compiler in Kotlin 1.5. The new IR backend should improve performance, according to JetBrains, while providing a sound infrastructure to add support for new language features. The new compiler is not the only new feature that Kotlin 1.5 adds to its JVM backend, which also improves type nullability handling and compilation of SAM adapters and lambdas.

Kotlin 1.5 requires at least Android Studio 4.2, or Arctic Fox Canary 15.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK