arekolek
05/13/2018, 7:23 PMwhen
expression is exhaustive?
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
}
}
Andreas Sinz
05/13/2018, 7:24 PMarekolek
05/13/2018, 7:24 PM@IntRange
from support annotations (but to get compile time and runtime precondition check)Andreas Sinz
05/13/2018, 7:25 PMelse -> throw Exception
frellan
05/13/2018, 8:10 PMelse -> {}
Andreas Sinz
05/13/2018, 8:25 PMfrellan
05/13/2018, 8:30 PMAndreas Sinz
05/13/2018, 8:45 PMfrellan
05/13/2018, 9:20 PMvar 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
arekolek
05/13/2018, 9:31 PMreturn 10 * when(amount.unit.type) {
Type.VOLUME, Type.PACKAGE, Type.PIECE -> food.density
Type.SPECIAL -> special.density
Type.PORTION -> portion.density
else -> 1
}
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)frellan
05/13/2018, 9:35 PM