Is there any way to make Kotlin understand that th...
# announcements
a
Is there any way to make Kotlin understand that this
when
expression is exhaustive?
Copy code
fun test(x: Int): Int {
        assert(x in 1..3) // doesn't help
        if (x !in 1..3) throw IllegalArgumentException()  // doesn't help
        return when (x) {
            1 -> -1
            2 -> 0
            3 -> 1
        }
    }
a
nope
a
I guess I can just move the assertion to else
👌 3
but I was looking for something like
@IntRange
from support annotations (but to get compile time and runtime precondition check)
a
yes, I'd just have a
else -> throw Exception
f
This is so anoyying, I get that it is a great warning sometimes but yeeech, I just want to use it like a bunch of if statements, not it always beeing exhaustive, currently I have this in my codebase
else -> {}
a
@frellan non-exhaustive in the sense that you ignore some cases on purpose?
f
I have a number, and depending on whether an enum is some types, i multiply that number by different factors, I first define the number and only the types that match should modify it, I used a java switch before migrating my codebase
I dont want to initialise it in every when clause, thats just unnecessary
a
can you give me a small example?
f
Copy code
var multiplier = 10
when (amount.unit.type) {
  Type.VOLUME -> {
    multiplier *= food.density
  }
  Type.PACKAGE -> {
    multiplier *= food.density
  }
  Type.PIECE -> {
    multiplier *= food.density
  }
  Type.SPECIAL -> {
    multiplier *= food.density
  }
  Type.PORTION -> {
    multiplier *= food.density
  }
  else -> {}
}
return multiplier
Its different for every type, obviously. And there is more types than what I check for but for the rest of them I dont want to modify it
a
In your case I'd do:
Copy code
return 10 * when(amount.unit.type) {
  Type.VOLUME, Type.PACKAGE, Type.PIECE -> food.density
  Type.SPECIAL -> special.density
  Type.PORTION -> portion.density
  else -> 1
}
(or you could actually list all remaining enum values, instead of
else
, if there's not many of them, I think there's even an intention action that does that for you if you don't have the else branch but you need one)
f
Hmm, yeah its an expression, forgot that. Maybe I can do that, thanks for the tip!