Hello, I feel I should know this, but why do I ne...
# getting-started
t
Hello, I feel I should know this, but why do I need the
else
in this
when
? and please, elaborate on the "type erasure" answer 😄
Copy code
sealed interface Test

class Test1: Test
class Test2: Test

fun <T: Test> T.call() = when (this) {
  is Test1 -> Unit
  is Test2 -> Unit
  else -> throw IllegalArgumentException("why?")
}
Thanks 🙂
j
You theoretically don't need it, but the Kotlin compiler doesn't go that far (yet). There is an issue about this: https://youtrack.jetbrains.com/issue/KT-21908/Support-when-exhaustiveness-checking-for-generic-type-parameter-with-sealed-class-upper-bound And this is going to be fixed in Kotlin 2.1 (if I understand correctly)
t
thanks, I thought there was going to be a yourtrack already, just did not find myself
y
If you do
when (this as Test)
It will work then I believe!
t
uh, trickster 😄 (it does work as expected then)