Hello, I’m sort of stuck or I’m not sure how to se...
# getting-started
Hello, I’m sort of stuck or I’m not sure how to search for answer to this question, but I’m writing an interface and abstract class that works with a generic type. The implementation works with SQS and uses kotlinx.serialization to de/serialize messages to JSON. But the serialization library is causing this compiler error,
Cannot use 'T' as reified type parameter. Use a class instead.
. Is there a way to keep `sendMessage/receiveMessage`` in the abstract class? Or do I have to move it into the concrete implementation?
You just can't use generic parameter like that because of JVM type erasure. Kotlin has a workaround for some cases - inline functions with reified type parameters.
function is a reified one but you can't pass erased T there. Probably there is overload for this function which accepts KClass parameter. So the solution here is to store reference to a KClass instance in constructor (
private val clazz: KClass<T>
) and pass concrete classes in inheritors. After that you can pass it to
if there is such overload
👍 1
Thanks. I’ll have to re-read that documentation again. I remember reading a bit about type erasures, but I didn’t really get it. I’ll see if I can find that alternate serialize function that accepts a class.