Hello everyone, Can we do something like this in ...
# announcements
g
Hello everyone, Can we do something like this in
Kotlin Common
instead of JVM?
Copy code
class MyClass<MyGeneric> {
    lateinit var myGenericClass: Class<MyGeneric>

    fun init() {
        myGenericClass =
            (javaClass.genericSuperclass as ParameterizedType)
                .actualTypeArguments[0] as Class<MyGeneric>
    }
}
Get the generic type?
r
Why not just use something like
Copy code
class MyClass<T>(val type: KClass<T>)
inline fun <reified T> MyClass() =
    MyClass(T::class)
👍 2
g
I’d like to create a special convertor, so, preferably, I’d like to write something like
Copy code
convert<List<Int>>(myStringVariable)

convert<Set<Long>>(myStringVariable)

convert<Map<String, Double>>(myStringVariable)

and so on
I agree that is possible to use something like that, but since we can achieve the result above in the JVM I was wondering about kotlin common. I’m also thinking about using something like:
Copy code
convert<Set<Long>>(myStringVariable, Set::class, Long::class)
r
I'd just stick with passing in the classes. From my experience, it's better to just understand and work with erasure instead of fighting it, but that's just my opinion.
g
I agree, either way I decided to file an issue to open this possibility for the future: https://youtrack.jetbrains.com/issue/KT-39631
e
the last code snippet will need a higher-kind type, and kotlin doesn't suport it currently :(