07-20-2023, 04:36 PM
Is it possible to provide a compound boolean expression in the `when(subject) { ... }` block?
The following will not compile
<!-- language: lang-kotlin -->
val num: Any = 2
when(num) {
/* compound boolean expression that uses implicit subject expression
* and an independent expression */
is Number && true -> println("TRUE")
else -> println("FALSE")
}
The following will compile, but results in `FALSE`. Is this the expected behavior?
<!-- language: lang-kotlin -->
val num: Any = 2
when(num) {
num is Number && true -> println("TRUE")
else -> println("FALSE")
}
I have a long list of *when* conditions and most of them only use the implicit subject but a couple need secondary conditions
The following will not compile
<!-- language: lang-kotlin -->
val num: Any = 2
when(num) {
/* compound boolean expression that uses implicit subject expression
* and an independent expression */
is Number && true -> println("TRUE")
else -> println("FALSE")
}
The following will compile, but results in `FALSE`. Is this the expected behavior?
<!-- language: lang-kotlin -->
val num: Any = 2
when(num) {
num is Number && true -> println("TRUE")
else -> println("FALSE")
}
I have a long list of *when* conditions and most of them only use the implicit subject but a couple need secondary conditions