While implementing functions that support generic type arguments, we often deal with the need to provide additional information about object types at runtime. On the JVM platform, types have their representations in the Class<T> class instances. For example, we can face such a need while parsing JSON formatted data to the Kotlin class instances using the Gson library:
data class ApiResponse(val gifsWithPandas: List<ByteArray>)
data class Error(val message: String)
fun parseJsonResponse(json: String): ApiResponse {
Gson().fromJson(json, ApiResponse::class.java)
}
Normally, we can't access the generic type argument at runtime because of a JVM types erasure. However, Kotlin allows you to overcome this limitation because it preserves the type argument at runtime. In this recipe, we are going to tune up Gson&apos...