在我的android项目中:
import retrofit2.Call
import retrofit2.Callback
fun getAdvertising(callback: Callback<List<Advertising>>) {
val call = tangoRestClient.advertising
executeAsync(call, callback)
}
private fun executeAsync(call: Call<*>, callback: Callback<*>) {
call.enqueue(callback) // compile error
}
但是我得到了编译错误:
> Task :app:generateDebugSources
> Task :app:validateSigningDebug
> Task :transport:compileDebugKotlin FAILED
e: \TransportService.kt: (127, 14): Out-projected type 'Call<*>' prohibits the use of 'public abstract fun enqueue(p0: Callback<T!>!): Unit defined in retrofit2.Call'
FAILURE: Build failed with an exception.
答案 0 :(得分:3)
是的,Call
和Callback
的类型参数应该相同。您必须这样编写executeAsync
函数:
private fun <T> executeAsync(call: Call<T>, callback: Callback<T>) {
call.enqueue(callback) // no compile error
}
这将确保类型参数相同