https://kotlinlang.org logo
#compiler
Title
# compiler
j

Jan Skrasek

08/20/2022, 8:14 AM
I'd like to check if reified generic T is implementing an interface.
Copy code
interface Destination {}
interface Dialog : Destination {}

inline fun <reified T : Destination> add() {
   val isDialog1 = T is Dialog // does not compile
   val isDialog2 = Dialog::class.java.isAssignableFrom(T::class.java) // works, but uses Java reflection
   val isDialog3 = (typeOf<T>().classifier as KClass<T>).isSubclassOf(Dialog::class) // works, but uses Kotlin full reflection, doesn't it?
}
Isn't there any compiler optimized way to do this? But THB, I'm not including kotlin full reflection to my dependencies, so maybe the third way IS optimized?
4 Views