Julius Marozas
12/04/2020, 12:08 PMsealed class Fruit {
object Apple : Fruit()
object Orange : Fruit()
}
fun <T : Fruit> eat(fruit: T): Unit =
// 'when' expression must be exhaustive, add necessary 'else' branch
when (fruit) {
is Fruit.Apple -> {}
is Fruit.Orange -> {}
}
This is just a contrived example, I know that I could simply inline 'T' with 'Fruit'. I am more interested to understand why Kotlin fails to see that the 'when' expression is exhaustive.Vampire
12/04/2020, 12:17 PMJoris PZ
12/04/2020, 12:19 PMwhen(fruit as Fruit)
but then the compiler warns you 'No cast needed' πJulius Marozas
12/04/2020, 12:27 PMVampire
12/04/2020, 12:29 PMJoris PZ
12/04/2020, 12:30 PMJoris PZ
12/04/2020, 12:30 PMwhen generics
and got lucky π )Julius Marozas
12/04/2020, 12:31 PMVampire
12/04/2020, 12:31 PMTobias Berger
12/04/2020, 1:43 PMJulius Marozas
12/04/2020, 2:54 PMTobias Berger
12/04/2020, 3:02 PM