'How do I make a generic function with generic function as parameter in Kotlin?
I have about 20 files that look like the Red and Blue section from the picture above, now I want to add
private val requestDataSource: RequestDataSource parameter (Green line 15) and call requestDataSource.post() (Green line 20) to all of them.
I've attempted to create a generic function with a custom parameter P, custom return type RET and a function parameter so I can use it as I do in the sendEz function but it's not working.
I am getting an error on the function parameter:
Type mismatch. Required: (TypeVariable(P)) → ApiRequestResponse<TypeVariable(RET)> Found: Boolean Function invocation 'send(...)' expected
I don't understand, I don't want to invoke it on line 30m and at the same time it looks like it's seen as invoked as the found parameter is a boolean (the return type of service.sen(emnail)) but I am not calling it there...
Is this achievable? If so, how?
Code here:
class EmailDataSource @Inject constructor(
private val dao: EmailDao,
private val service: EmailService,
private val requestDataSource: RequestDataSource,
): BaseDataSource<EmailModel, EmailDataModel>(dao) {
suspend fun send(email: EmailModel): ApiRequestResponse<Boolean> {
return try {
val response = service.send(email)
requestDataSource.post()
ApiRequestResponse.Success(response)
} catch (e: Throwable) {
val ioe = IOException("Error sending email", e)
Timber.w(ioe)
ApiRequestResponse.Error(ioe)
}
}
suspend fun sendEz(email: EmailModel): ApiRequestResponse<Boolean> {
return call<EmailModel, Boolean>(service.send, email, "Error sending email")
}
suspend fun <P: Any, RET: Any> call(
function: (parameter: P) -> ApiRequestResponse<RET>,
parameter: P,
error: String = "Error making the request"
): ApiRequestResponse<RET> {
return try {
val response = function(parameter)
requestDataSource.post()
response
} catch (e: Throwable) {
val ioe = IOException(error, e)
Timber.w(ioe)
ApiRequestResponse.Error(ioe)
}
}
}
Solution:
protected suspend fun <P: Any, RET: Any> call(
function: KSuspendFunction1<P, RET>,
parameter: P,
error: String = "Error making the request"
): ApiRequestResponse<RET> {
val result = function(parameter) // call the function
}
/** And call it like this */
suspend fun get(param: String, param2: Boolean): ApiRequestResponse<WHAT service::get RETURNS> {
return call(service::get, param, param2, "Error message")
}
/** Where service is the retrofit interface */
@GET("Endpoint/{date}/{includeStops}")
suspend fun get(
@Path("date") date: String,
@Path("stops") stops: Boolean
): List<Model>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

