Hello, World! I’m in a `when (e)`, (with e being ...
# announcements
b
Hello, World! I’m in a
when (e)
, (with e being an instance of
MyEnum
) and for the branches, I’m allowed to put something that is not one of the possible values of
MyEnum
(I can put values of other enums). I find this extremely surprising 🙂 Thoughts?
h
can u show us an example
a
yeah it would be nice to have at least a warning that this is an unreachable branch
b
Copy code
when (transaction.status) {
                    TransactionStatus.PENDING -> TODO()
                    TransactionStatus.COMPLETED -> TODO()
                    TransactionStatus.DECLINED -> TODO()
                    TransactionStatus.UNKNOWN -> TODO()
                    TransactionActivityTag.TRANSPORT -> TODO()
a
(or maybe it’s not actually unreachable)
b
compiles
h
is there any warnings?
b
nope
how come I’m not allowed to put, say, a
String
but another type that happens to be an enum. I’m allowed?
a
I remembered there was a similar discussion https://kotlinlang.slack.com/archives/C0922A726/p1530087621000037
b
wow 😱
ok,
when
on `enum`s is broken in Kotlin 😄 Not scary at all!
oh wait it says it’s fixed 🙂 https://youtrack.jetbrains.net/issue/KT-24708
(we’re not on 1.3 yet)
a
and this one https://youtrack.jetbrains.net/issue/KT-22043 looks like what would fix the thing you originally mentioned
👍 1
b
(in my case, we have the same value name in 2 enums, and I “static import” them both - hilarity ensued)
😱 1
m
The when is much more strict if the result is used. Still an issue here that may be addressed. Would be curious to know what happens if you assign the when to a variable.
a
Same thing happens
image.png
😱 1