What's new in Kotlin 2.5.0-Beta1
The Kotlin 2.5.0-Beta1 release is out! Here are some details of this EAP release:
Language: Stable name-based destructuring in
only-syntaxmode and new experimental companion extensions and blocksStandard library: New experimental functions for simplifying common patterns with
ifexpressionsKotlin/JS: Support for the
es2020targetKotlin compiler: More consistent inline function behavior during
.klibcompilation
Update to Kotlin 2.5.0-Beta1
The latest version of Kotlin is included in the latest versions of IntelliJ IDEA and Android Studio.
To update to the new Kotlin version, make sure your IDE is updated to the latest version and change the Kotlin version to 2.5.0-Beta1 in your build scripts.
Language
Kotlin 2.5.0-Beta1 stabilizes two language features introduced in earlier releases. It also introduces experimental companion extensions and companion blocks.
Stable language features
Kotlin 2.3.20 and 2.4.0 introduced a few language features as Experimental. We're happy to announce that the following language features are now Stable in this release:
Name-based destructuring in
only-syntaxmode.In this mode, the "old" destructuring syntax
val (x, y)keeps its position-based behavior, while the "new" syntax(val x, val y)performs name-based destructuring.
Companion extensions and blocks
Kotlin 2.5.0-Beta1 introduces companion extensions and companion blocks.
Previously, to declare extensions, functions, and properties that you could access through a type's name, the type needed to have a companion object. Companion extensions and blocks remove this requirement and let you:
Declare a companion extension by adding the
companionmodifier to a top-level extension, even if the type it extends has no companion object.Declare functions and properties in a
companion {}block inside a class or interface without creating an object instance. On platforms that support static members, the compiler generates these declarations as static members. As a result, you don't need to annotate them with@JvmStaticon the JVM.
Here's an example that declares UnitX as a companion extension and Zero in a companion block:
For more information about the design, see the feature's KEEP.
Companion extensions and blocks are Experimental. To opt in, add the following compiler option to your build file:
We would appreciate your feedback in YouTrack.
Standard library: New functions for simplifying common patterns with if expressions
Kotlin 2.5.0-Beta1 introduces new standard library functions that let you inspect a Boolean value before returning it or return a nullable result depending on that value.
Previously, these patterns required explicit if expressions with else branches. You can now simplify them with the following functions:
onTrue()runs a specified code block when aBooleanvalue istrueand returns the original Boolean value.onFalse()runs a specified code block when aBooleanvalue isfalseand returns the original Boolean value.ifOrNull()runs a specified code block and returns its result if theBooleanvalue istrue. If the value isfalse, the function returnsnullwithout running the block.
These functions are Experimental and require opt-in with the @OptIn(ExperimentalStdlibApi::class) annotation or the -opt-in=kotlin.ExperimentalStdlibApi compiler option.
Here's an example:
We would appreciate your feedback in YouTrack.
Kotlin/JS: Support for the es2020 target
Kotlin 2.5.0-Beta1 adds the es2020 target to the Kotlin/JS compiler and Gradle plugin. Previously, only the es5 and es2015 targets were available, and support for newer JavaScript features, such as BigInt, had to be enabled separately targeting ES2015. By targeting ES2020, you can use all supported JavaScript features up to ECMAScript 2020, including BigInt, without additional configuration.
To enable the new target, set target to es2020 in the compilerOptions block:
Kotlin compiler
Kotlin 2.5.0-Beta1 brings more improvements to function inlining during .klib compilation and experimental features such as improved type inference performance.
Consistent cross-module function inlining during klib compilation
Kotlin 2.4.0 enabled consistent intra-module function inlining on Kotlin/Native, Kotlin/JS, and Kotlin/Wasm during .klib compilation. The consistency of function inlining across different Kotlin platforms makes it easier to provide compatibility guarantees.
Kotlin 2.4.0 also introduced the possibility to enable cross-module inlining during .klib compilation, ensuring that all inline functions in a project are consistently inlined. Kotlin 2.5.0-Beta1 enables cross-module inlining by default.
If you face unexpected problems with this feature, you can disable it with the following command-line compiler option:
Please share your feedback and report any problems in YouTrack.
Improved type inference performance
Kotlin 2.5.0-Beta1 improves compiler performance by reducing the number of constraints generated during type inference. Previously, complex generic code could generate excessive constraints, causing compilation or IDE analysis to hang. This change may affect type inference in certain edge cases, particularly those involving builder inference or complex platform types with unusual bounds. As a result, the compiler may infer different types, select different overloads, or report different diagnostics. These differences can be an expected result of the improvement.
The functionality is enabled by default. To restore the previous type inference behavior, use the -XXLanguage:-EliminateSecondKindIncorporation option.
We would appreciate your feedback in YouTrack.
Breaking changes and deprecations
Kotlin 2.5.0-Beta1 introduces a warning as the first step to raising the minimum JDK version required to run the Kotlin compiler from JDK 8 to JDK 17. We're raising the minimum required JDK to speed up development and give the compiler access to new libraries that require newer Java versions. JDK 17 has a long support window and helps us maintain compatibility with newer versions of Gradle and Maven. Opt out of the warning with the -Xallow-pre-17-runtime-jdk compiler option. This option will be removed in Kotlin 2.5.20 or 2.6.0 when JDK 17 becomes mandatory.
If you have difficulty upgrading your project, share your experience on YouTrack or reach out directly to developers on Kotlin Slack. Get an invite and join the #compiler channel.