Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 372 Vote(s) - 3.6 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does the Kotlin "when" expression support compound boolean expression when using a subject?

#1
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
Reply

#2
Regarding the first one: looking at the [`when` grammar](

[To see links please register here]

) it is intended. Only `in` and `is` (and their negative counterparts) are allowed directly. Otherwise you only have expressions.

Now regarding your second one it may help to transform it to an `if`/`else`-statement first. Basically your `when` will look as follows then:

if (num == ((num is Number) && true)) println("TRUE")
else println("FALSE")

If looking at this it becomes clear why it always prints `FALSE`. While `num is Number` becomes `true` and `true && true` is still `true`, the `num == true` is false as `num` isn't even a boolean ;-)

Or in other words: your "subject" in the `when` is compared against each of the conditions (thanks [Bwvolleyball](

[To see links please register here]

) for the comment).

---

What however I don't really understand (but that could also be my misunderstanding of the grammar)... looking at the grammar it seems as if the following would be possible:

when {
is Number -> TODO()
}

but as one could expect: it doesn't... but the last statement is rather a side-note.
Reply

#3
//First case
//Prints TRUE if the num value is numeric and true.Anything else prints FALSE
val num: Any = 2
when {
num is Number && true -> println("TRUE")
else -> println("FALSE")
}
//Second case
//Prints true if the value is numeric OR true.Anything else prints FALSE
val num: Any = 2
when(num){
is Number , true -> println("TRUE")
else -> println("FALSE")
}


Reply

#4
> Is it possible to provide multiple boolean conditions when using a `when(subject) { ... }` expression?

Yes, you can separate multiple [*when conditions*][1] using a `,` (comma).

For instance, the following `when`s have the exact same behavior. [Runnable playground demo here][2].

```kotlin
fun withoutComma(arg: Any?): String {
return when(arg) {
is String -> "FOO!" // returned when arg is an instance of String
true -> "bar!" // returned when arg == true
is Long -> "FOO!" // returned when arg is an instance of Long
false -> "bar!" // returned when arg == false
null -> "FOO!" // returned when arg == null
is Int -> "bar!" // returned when arg is an instance of Int
else -> "else!"
}
}

fun withComma(arg: Any?): String {
return when(arg) {
is String, is Long, null -> "FOO!" // returned when arg is an instance of String, or when arg is an instance of Long, or when arg == null
true, false, is Int -> "bar!" // returned when arg == true, or when arg == false, or when arg is an instance of Int
else -> "else!"
}
}

fun main() {
listOf("s", true, 999L, false, null, 123, emptyList<Any>()).forEach {
println("$it -> withoutComma: ${withoutComma(it)}")
println("$it -> withComma: ${withComma(it)}")
}
}

// Prints:
// s -> withoutComma: FOO!
// s -> withComma: FOO!
// true -> withoutComma: bar!
// true -> withComma: bar!
// 999 -> withoutComma: FOO!
// 999 -> withComma: FOO!
// false -> withoutComma: bar!
// false -> withComma: bar!
// null -> withoutComma: FOO!
// null -> withComma: FOO!
// 123 -> withoutComma: bar!
// 123 -> withComma: bar!
// [] -> withoutComma: else!
// [] -> withComma: else!
```


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
If you want to have multiple conditions for the when statement to evaluate, you can also create a new variable and check all your cases when assigning it a value. It’s not always the best case if you’re working on a very large app and have to be conscious about memory, but it makes your code much more readable and concise

ex:

/*
Num is evaluated for whether it is greater than, less than, or equal to 0
and the corresponding String value is assigned to isNumberPositiveNegativeOrZero.
*/

val isNumberPositiveNegativeOrZero: String = when {
num > 0 -> “number is positive”
num < 0 -> “number is negative”
else -> “number is zero”
}

This can, of course, be expanded into however many cases you want to check.
(This is my first time answering a question on here so please excuse any accidents)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through