Can anyone explain this behavior to me? [Solved]
# announcements
t
Can anyone explain this behavior to me? [Solved]
r
Line 1 - false is not null, so false Line 2 - false is still not null, so false != ScreenStates.Static is true
Unlike groovy, in kotlin the elvis operator is only for null defaulting, not for truthiness.
t
but in Line 2 why get "state != ScreenState.Static" called in the first place
r
It isn’t. false ?: state is evaluated. Then result of that is compared.
t
oh ok thanks now it makes sense
But i find it still very unintuitive In my mind it would be like a logical operator where the right side only gets evaluated if the left is null
r
It is. It’s just the right side is just state. Left to right evaluation - imagine ?: and != are methods.
t
ow now i get it so "false.notNull(state) != ScreenStates.Static" equals true It took a while 😅 but thanks for the explanation 🙏
👍 1
k
Hold on, isn't ScreenStates.Static an enum? Then why isn't it a compile-time error to compare it with a boolean?
t
I would guess because the return type of ?: is Any at compile time and then it's no problem to compare Any with an Enum