Kotlin 2.1 Released: What's Changed in the Programming Language
JetBrains has officially announced the release of the stable version of Kotlin 2.1. This is the first major release since version 2.0, which came out in May 2024. The new version brings many improvements that have affected both the basic syntax and the infrastructure for multiplatform development. Let's break down the most significant innovations.
Key Changes in the Language
Developers focused on increasing stability and expanding the language's capabilities. Main new features:
- Stable non-local break and continue mode. The previously experimental feature for inline lambdas has become final. Now you can break out of outer loops from within lambda expressions (e.g., in
forEach) without additional wrappers. This simplifies writing clean code. - Subtypes of discriminated unions (experimental). Support for subtypes of sealed classes and interfaces has been introduced. Now you can declare sealed interfaces and inherit them in classes while maintaining strict when-expression checking.
- Improved Java 21 support. The Kotlin compiler now correctly handles new Java constructs such as
record patternsandswitchwith patterns. This is especially important for projects migrating to newer versions of Java.
Updates in Kotlin Multiplatform
Kotlin 2.1 continues the development of the multiplatform ecosystem:
- Stable Kotlin/Wasm. WebAssembly support has become final. Now you can compile code to Wasm, run it in the browser or on the server via Wasmtime.
- Compilation speedup for Kotlin/Native. Work with LLVM has been optimized. According to JetBrains estimates, build time for native projects has been reduced by 15–20%.
- New artifact for Android. Support for Android Gradle Plugin 8.7 has been added, and compatibility with Jetpack Compose libraries has been improved.
Changes in the Standard Library
Several useful functions have appeared in stdlib:
- The
buildMapandbuildSetfunctions. Now you can create immutable collections using a builder, similar tobuildList. This makes code safer and more expressive. - New methods for working with dates.
LocalDate.daysUntilandLocalDate.monthsUntilhave been added. They simplify calculating the difference between dates without third-party libraries. - Improved work with nullable types. The
?.letfunction now correctly handles nested calls without creating temporary variables.
Performance and Developer Improvements
JetBrains has also paid attention to developer tools:
- Smart autocompletion in IntelliJ IDEA. The Kotlin plugin has been updated to version 2.1. Now the IDE offers more relevant autocompletion options based on context.
- Optimization of the K2 compiler. Code analysis and error output have been accelerated. This is especially noticeable on large projects with thousands of files.
- New performance inspector. The built-in analyzer suggests