nkiesel
02/10/2021, 9:02 PMimport kotlin.random.Random
enum class Priority { LOW, MEDIUM, HIGH }
fun main() {
val priority = Priority.values()[Random.nextInt(Priority.values().size)]
val s1 = when (priority) {
Priority.LOW -> 1
Priority.MEDIUM -> 2
Priority.HIGH -> 3
}
val s2: Int
if (priority == Priority.LOW) {
s2 = 1
} else {
s2 = when(priority) {
// Priority.LOW -> throw IllegalStateException()
Priority.MEDIUM -> 2
Priority.HIGH -> 3
}
}
println("s1=$s1 s2=$s2")
}
nkiesel
02/10/2021, 9:04 PMephemient
02/10/2021, 9:05 PMnkiesel
02/10/2021, 9:16 PMnanodeath
02/10/2021, 9:42 PMwhen
expression is sufficientnkiesel
02/11/2021, 9:23 AMval s1 = when(value) {... }; ... val s2 = when(value) { ... }
. I do not need these s1
etc for the third case. Thus, I changed the code to if (value == Case.SIMPLE) { ... } else { ... complex code from above ...}
. However, I still now have to add branches for Case.SIMPLE
in all the `when`expressions. I could of course bundle that branch with any of the existing ones, but that makes the code hard to read.