Reputation: 1005
I am trying to pass a generic object
to an override
method that implements methods from an interface
.
Unfortunately, I am getting an error when attempting to implements this. The error is as follows:
Override by a function with reified type parameter
Heres is my code
class ServiceVolley : ServiceInterface {
val TAG = ServiceVolley::class.java.simpleName
val basePath = "http://192.168.68.104:3005/"
override inline fun <reified T> post(
resource: String,
payload: JSONObject,
crossinline onSuccess: (response: JSONObject?) -> Unit,
crossinline onError: (statusCode: Int) -> Unit
) {
val jsonObjReq = object : JsonObjectRequest(Method.POST, basePath + resource, payload,
Response.Listener<JSONObject> { response ->
Klaxon().parse<T>(response.toString())
onSuccess(response)
},
Response.ErrorListener { error ->
onError(error.networkResponse.statusCode)
}) {
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers["Content-Type"] = "application/json"
headers["withCredentials"] = "true"
return headers
}
}
BackendVolley.instance?.addToRequestQueue(jsonObjReq, TAG)
}
}
interface ServiceInterface {
fun <T> post(resource: String, payload: JSONObject, onSuccess: (response: JSONObject?) -> Unit, onError: (statusCode: Int) -> Unit)
}
and I call this function with the following
apiController.post<User>("user/sign-in", payload,
{ user: User? ->
Log.d("RESPONSE", user.toString());
},
{ statusCode: Int ->
Log.d("ERROR", statusCode.toString())
})
Upvotes: 1
Views: 4134
Reputation: 170899
See this issue: Overridding by inline function should be a warning, overriding by a function with reified type parameter should be an error.
Basically: with virtual functions, which function is called is determined at runtime; so they can't be inlined because the compiler doesn't know which function to inline. When you do something like
interface A {
fun f(t: Int)
}
class AImpl : A {
override inline fun f(t: Int) {
println(t.toString())
}
}
val a: A = AImpl()
a.f(1)
the non-inlined version of AImpl.f
gets called.
But functions with reified
must be inlined, or it won't work. So they can't be used to override.
Upvotes: 2