Paul Krill
Editor at Large

Kotlin previews guard conditions in when expressions

news
Dec 3, 20243 mins

Preview feature in Kotlin 2.1.0 lets developers include more than one condition for branches in when expressions and statements with subjects.

Profile side photo of concentrated skilled web expert afro american girl sit evening desk use computer netbook work java script html back-end algorithm in workstation workplace
Credit: Roman Samborskyi / Shutterstock

JetBrains has released Kotlin 2.1.0, an update to the Kotlin programming language that previews guard conditions and has compiler updates offering flexibility around checks.

Released November 27, Kotlin 2.1.0 can be installed as an update by following the instructions at kotlinlang.org.

Kotlin 2.1.0 introduces several language improvements in preview. One preview feature allows developers to use guard conditions in when expressions with subjects. Guard conditions let developers include more than one condition for branches of a when expression, making complex control flows more explicit and concise while also flattening code structure, JetBrains said. A second preview feature is the ability to use non-local break and continue. This feature expands the tool set developers can use in the scope of inline functions and reduces boilerplate code. A third preview feature introduces support for multi-dollar string interpolation, improving how the dollar sign ($) is handled within string literals. This helps with contexts that require multiple dollar signs, such as templating engines, JSON schemas, or other data formats, JetBrains said.

Also in Kotlin 2.1.0, Kotlin Multiplatform introduces basic support for Swift export and makes it easier to publish Kotlin Multiplatform libraries. Swift export allows for exporting Swift sources directly to the Swift interface without using Objective-C headers. Improvements around the Gradle build tool pertaining to Kotlin Multiplatform, meanwhile, stabilize a new DSL for configuring compiler options. An Isolated Projects feature to improve Gradle performance is included in a preview.

The K2 compiler in Kotlin 2.1.0 adds the ability to suppress warnings globally and enables additional checks, including extra declaration, expression, and type checks that are usually not crucial for compilation but can be useful for validating use cases such as REDUNDANT_NULLIBLE and CAN_BE_VAL. Other improvements in Kotlin 2.1.0:

  • Kotlin/Native upgrades iosArm64 target support including the cinterop caching process and other updates.
  • Kotlin/Wasm supports incremental compilation for Wasm targets. In development tasks, the compiler now recompiles only files relevant to changes from the last compilation, reducing compilation time. Also, debugging has been improved for Kotlin/Wasm and the size of Kotlin/Wasm binaries has been reduced.
  • Kotlin/JS adds support for generating ES2015 arrow functions, such as (a, b) => expression, instead of anonymous functions.

Kotlin 2.1.0 follows the May release of Kotlin 2.0.0, which brought the new K2 compiler to a stable state. Kotlin plugins that support version 2.1.0 are bundled in the latest IntelliJ IDEA and Android Studio IDEs.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author