Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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>
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.