Reified type parameters for inline classes

While this is not supported by Kotlin in this form, you can still simulate it without sacrificing the convenient API of your class this way:

fun main() {
    println(Blah<Int>("foo").bloh()) 
    // prints Int: foo
}
    
    
class Blah<T : Any>(val name: String, val clazz: kotlin.reflect.KClass<T>) {
     fun bloh(): String {
          return "${clazz.simpleName}: $name"
     }

     companion object {
          inline operator fun <reified T : Any> invoke(name: String) = Blah(
              name, T::class
          )
     }
}
3 Likes