Is there a way to exhaust a reified generic using when statement in Kotlin?

Viewed 39

I want to perform different actions based on the type of generic the function is running with. It feels to me like the compiler has all the information to not use else but the code will not compile without it. Am I missing something or is there another more obvious way to do that in Kotlin? Example:

sealed class A {
    data class A1(val a: String): A()
    data class A2(val a: String): A()
}

inline fun <reified T: A> function() {
    return when (T::class) {
        A.A1::class -> {  }
        A.A2::class -> {  }
        else -> {  } // Problem: Does not compile without the else branch.
    }
}
0 Answers
Related