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)
}
}
}
A .
is syntax for invoking it. ::
is used for just passing references to functions.
So try putting service::send
instead of service.send
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.