Ray Rahke
02/24/2024, 2:07 PMval result = when {
guess > answer -> "Lower!"
guess < answer -> "Higher!"
guess == answer -> "Yes!"
}
it says it is not exhaustive. it logically is.Harold Martin
02/24/2024, 3:43 PMguess
and answer
?Ray Rahke
02/24/2024, 3:50 PMCasey Brooks
02/24/2024, 3:55 PMelse ->
condition, or turn your final ==
condition into the else
Ray Rahke
02/24/2024, 3:56 PMRay Rahke
02/24/2024, 3:56 PMCasey Brooks
02/24/2024, 3:57 PMwhen
without a subject, the branches are arbitrary expressions so you'll always need to provide an else
. It can only be smart when it has a specific value it's checking conditions for (when(guess)
), and then only in some specific circumstancesRay Rahke
02/24/2024, 3:57 PMRay Rahke
02/24/2024, 3:58 PMRay Rahke
02/24/2024, 3:58 PMephemient
02/24/2024, 4:04 PMvalue class LessThan(val value: Int) {
operator fun contains(other: Int): Boolean = other < value
}
value class GreaterThan(val value: Int) {
operator fun contains(other: Int): Boolean = other > value
}
when (guess) {
in LessThan(answer) -> ...
in GreaterThan(answer) -> ...
else -> ...
}
or using ranges such as Int.MIN_VALUE..<answer
, but the compiler still requires else