Guard conditions

Create advanced control flows in when statements

View at original site


When expressions have always been powerful, but in Kotlin 2.1 it is becoming even more powerful with guards.

To enable it:

  • Ensure you are using Kotlin 2.1.0 or later in your project
  • Add -Xwhen-guards as a compiler argument

For IDE support, use the latest 2024.3 version of IntelliJ IDEA with K2 mode enabled.

Related Resources

Your First AI Agent in Kotlin
Your First AI Agent in Kotlin
Get started with Koog today!
Exposed inline value classes for Java
Exposed inline value classes for Java
Use value classes from Java code with Kotlin 2.2
Nested type aliases in Kotlin 2.2
Nested type aliases in Kotlin 2.2
Use type aliases inside other structures