添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
独立的打火机  ·  Direct bass : Vb.net ...·  4 月前    · 
乖乖的莲藕  ·  GitHub - ...·  4 月前    · 
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>
                What is EmailService? Looks like you want to pass the send function of EmailDataSource to the call function. Is it so? Why service.send then?
– Arpit Shukla
                Feb 2, 2022 at 9:33
                In that case, yes, but I have 20 other services that return other things.   Now I only need a better way for the KSuspendFunction1<P, RET> parameter to support function parameters instead of overloading with KSuspendFunction0, KSuspendFunction1, KSuspendFunction2...
– SKREFI
                Feb 2, 2022 at 19:34
                Thanks! I thought brackets is calling the function. Btw, do you know if there is something like a KSuspendFunctionN in kotlin.reflect?
– SKREFI
                Feb 2, 2022 at 19:36
        

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.