If I try to get
BaseResponse<Iterable<User>>::class
I get error error only classes are allowed on the left hand side of class literal. I have searched and still not found how to get generic classes's type in kotlin.
You can't -KClass
can only describe the class BaseResponse
, and you can get a KClass
instance that does that with BaseResponse::class
.
What you have however, BaseResponse<Iterable<User>>
, is a concrete type, which you can be represented as a KType
instance. KType
instances can be created, for example, with the createType
function. This would look something like this:
// User
val userType = User::class.createType()
// Iterable<User>
val iterableOfUserType = Iterable::class.createType(arguments = listOf(KTypeProjection.invariant(userType)))
// BaseResponse<Iterable<User>>
val responseType = BaseResponse::class.createType(arguments = listOf(KTypeProjection.invariant(iterableOfUserType)))
I made the choice of making the type parameters both invariant, there's also factory methods in KTypeProjection
to create covariant or contravariant types, as you need them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With