Workaround for reified T at class level

I already discussed this issue in an obsolete thread, however I leave you this snippet, I hope this helps.

import kotlin.reflect.KClass

fun main() {
    val test = Test<String>()
    println(test)
}

class Test<T : Any>(val type: KClass<T>) {

    override fun toString(): String = "Test<${type}>"

    companion object {
        inline operator fun <reified T : Any> invoke(): Test<T> = Test(T::class)
    }
}
4 Likes