有没有办法在 Kotlin 中使用 when 语句来耗尽具体化泛型?

Dan*_*tor 5 generics kotlin

我想根据函数运行的泛型类型执行不同的操作。在我看来,编译器拥有所有不使用的信息else,但如果没有它,代码将无法编译。我是否遗漏了一些东西,或者在 Kotlin 中是否有另一种更明显的方法可以做到这一点?例子:

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.
    }
}
Run Code Online (Sandbox Code Playgroud)