Kotlin:如何将具有不同参数的函数作为参数传递给其他函数

Kotlin: how to pass a function with varying arguments as a parameter to other function

提问人:Bartek Pacia 提问时间:9/26/2019 更新时间:10/3/2019 访问量:1352

问:

因此,我正在重写我的应用程序的代码,使其“干净”(层分离,遵循 Android 团队推荐的 MVVM 模式)

在这里,我有一个简单的 Retrofit 接口来与我的 API 进行通信

interface Api {

    @GET("comments")
    suspend fun getPlaceComments(@Query("placeId") placeId: String): Response<List<CommentResponse>>

    @POST("comments")
    suspend fun addPlaceComment(@Header("placeId") placeId: String, @Header("text") text: String): Response<Unit>

    @DELETE("comments")
    suspend fun deletePlaceComment(@Header("placeId") placeId: String): Response<Unit>
}

只是一个简单的 CRUD。

现在,再往上一层,我有了我的 SocialRepository。为了避免代码重复,我创建了一个通用方法,该方法采用挂起的 API 函数和 placeId 作为其参数。callSafely

class SocialRepository {
    private val client: Api = ApiClient.webservice

    private suspend fun <T> callSafely(
        apiMethod: suspend (placeId: String) -> Response<T>,
        placeId: String,
    ): T? {
        Log.d(TAG, "$apiMethod called safely")

        var response: Response<T>? = null

        try {
            response = apiMethod(placeId)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        if (response?.isSuccessful != true) {
            Log.w(TAG, "response.isSuccessful isn't true.")
        }

        return response?.body()
    }

    suspend fun getPlaceComments(placeId: String): List<CommentResponse>? {
        return callSafely(client::getPlaceComments, placeId)
    }

    suspend fun deletePlaceComment(placeId: String): Unit? {
        return callSafely(client::deletePlaceComment, placeId)
    }

    suspend fun addPlaceComment(placeId: String, text: String): Unit? {
        return callSafely(client::addPlaceComment, placeId, text) // HERE LIES THE PROBLEM
        // I can't pass additional data because the method signature won't match with what's defined in callSafely()
    }
}

现在,它运行良好,当然我也有我的 Activity 及其 ViewModel,并且 ViewModel 调用存储库中的方法等。这不重要。

重要的是,添加地点评论需要额外的数据,例如评论的实际文本。获取和删除注释只需要 placeId,而在添加注释时,其内容也是必需的。 我读到在 Kotlin 中传递函数是不可能的。我也不想用类似 a 的东西混淆所有 API 方法,这些东西大部分时间都是空的,只会造成混乱。textvarargList of params

我可以通过简单的方法复制 to 的代码并更改它,但这不是我想要的。我知道如何解决问题,但我不知道该怎么做.将来,我可能会添加更多需要额外数据的端点(除了),问题将再次出现。callSafelyaddPlaceCommentthe clean wayplaceId

在这种情况下你会怎么做?如何“正确”地写?

我什至不确定如何正确表达我正在寻找的东西,这就是为什么这篇文章如此漫无边际的原因。提前对不起。我真的希望你能帮助我。

Android 泛型 Kotlin 改造

评论


答:

1赞 kouadio figux 9/26/2019 #1

试试这个:

class SocialRepository {
private val client: Api = ApiClient.webservice

private suspend fun <T> callSafely(
    apiMethod: suspend (placeId: String) -> Response<T>,
    vararg stringParams: String,
): T? {
    Log.d(TAG, "$apiMethod called safely")

    var response: Response<T>? = null

    try {
        response = apiMethod(stringParams[0])
    } catch (e: Exception) {
        e.printStackTrace()
    }

    if (response?.isSuccessful != true) {
        Log.w(TAG, "response.isSuccessful isn't true.")
    }

    return response?.body()
}

suspend fun getPlaceComments(placeId: String): List<CommentResponse>? {
    return callSafely(apiMethod= client::getPlaceComments, stringParams=*arrayOf(placeId))
}

suspend fun deletePlaceComment(placeId: String): Unit? {
    return callSafely(apiMethod=client::deletePlaceComment, stringParams=*arrayOf(placeId))
}

suspend fun addPlaceComment(placeId: String, text: String): Unit? {
    return callSafely(apiMethod = client::addPlaceComment,stringParams= *arrayOf(placeId,text))
}

}

评论

2赞 a_local_nobody 9/27/2019
不要只发布代码,如果你只是发布代码,没有人会学到任何东西。解释解决方案可能与实际解决方案一样有用,因此请考虑将来添加更多解释。只是一个友好的提示:)
5赞 GV_FiQst 9/26/2019 #2

“清洁方式”是一个非常宽泛的概念。一切都取决于您的需求,没有“一种好的做事方式”。

在您的特定情况下,您有以下几种选择:

1) 类型别名

typealias ApiCall1<P, R> = suspend (P) -> Response<R>
typealias ApiCall2<P1, P2, R> = suspend (P1, P2) -> Response<R>

fun <P> callSafely(param: P, call: ApiCall1<P, YourResult>): YourResult
fun <P1, P2> callSafely(param1: P1, param2: P2, call: ApiCall2<P1, P2, YourResult>): YourResult

2) 瓦拉格斯

fun callSafely(vararg params: String, call: suspend (arr: Array<String>) -> YourResult {
   ...
   call(*params) 
   ...
}

3) Lambdas(更适合您的情况)

没有人强迫您使用方法引用。在需要时使用 lambda。但是将 lambda 作为“更干净”代码的最后一个参数。

private suspend fun <T> callSafely(
    placeId: String,
    apiMethod: suspend (placeId: String) -> Response<T>
): T?

suspend fun getPlaceComments(placeId: String): List<CommentResponse>? {
    return callSafely(placeId, client::getPlaceComments)
}

suspend fun deletePlaceComment(placeId: String): Unit? {
    return callSafely(placeId, client::deletePlaceComment)
}

suspend fun addPlaceComment(placeId: String, text: String): Unit? {
    return callSafely(placeId) { id -> client.addPlaceComment(id, text) }
}

评论

0赞 Bartek Pacia 9/27/2019
谢谢,我知道理想的解决方案不存在,但选项 3 正是我正在寻找的!