SKREFI
SKREFI

Reputation: 187

How do I make a generic function with generic function as parameter in Kotlin?

enter image description here

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>

Upvotes: 0

Views: 660

Answers (1)

Ivo
Ivo

Reputation: 23144

A . is syntax for invoking it. :: is used for just passing references to functions.

So try putting service::send instead of service.send

Upvotes: 2

Related Questions