Any idea why this ``` val newStage = wh...
# getting-started
d
Any idea why this
Copy code
val newStage = when {
                expiresInDays >= 2 -> 0
                expiresInDays in 0..1 -> 1
                expiresInDays < 0 -> 2
                else -> 3
            }
generates a warning
Condition 'expiresInDays < 0' is always true
in IDEA? (
expiresInDays
is of type Long.)
e
presuming
expiresInDays
is a
val
, the IDE inspection knows that the first 3 cases cover everything. Kotlin doesn't, though: https://youtrack.jetbrains.com/issue/KT-34548
d
I (and IDE IMO as well) am not talking about the last condition but about the 3rd one
expiresInDays < 0
And yes,
expiresInDays
is val
Thanks for the link! That would be my second question anyway 🙂
e
IDE is telling you that your code has the same effect as
Copy code
val newStage = when {
                expiresInDays >= 2 -> 0
                expiresInDays in 0..1 -> 1
                true -> 2
                else -> 3
            }
d
Ah, I see
e
if you only use
expiresInDays >= 2 ->
,
expiresInDays in 0..1 ->
, and
else ->
as the three cases, the compiler will know it's exhaustive and you also don't have IDE warnings
👍 1
k
I just realised that "else" could always be replaced by "true" in
when
statements/expressions, making "else" redundant (except after "if"). But "else" sounds more sensible anyway.
e
when writing guards in Haskell, it is conventional that the fallback case is
| otherwise ->
. that isn't a language keyword, it's just defined as
otherwise = True
.