I have a class declared as a generic API response data model:
data class APIResponse<out T>(val data: T?)
And I am us Retrofit2 to declare APIs. I just simplified it (ignored those annotations)
interface APIs {
fun getListCall(): Call<APIResponse<String>>
}
And I have a function to get the API call. However, i shows an error.
private fun getAPI(apis: APIs): Call<APIResponse<Any>> {
return apis.getListCall()
}
Type mismatch.
Required:
Call<APIResponse<Any>>
Found:
Call<APIResponse<String>>
because I remember if I put “out” modifier that could fix it as here says
https://kotlinlang.org/docs/reference/generics.html
I tested with other functions with similar declarations, it does not give me the error, becuase “out” modifier is added.
private fun getStringResponseList(): List<APIResponse<String>> {
return listOf(APIResponse("data"))
}
private fun getResponseList(): List<APIResponse<Any>> {
return getStringResponseList()
}
Scratch my head and still cannot figure it out…