我有一個比較來自兩個不同端點的回應的函式。它看起來像這樣:
suspend fun <I, T> multiplexOrShadow(
request: I,
v1ResponseStringGenerator: KFunction1<T, String> = ::getV1ResponseString,
v2ResponseStringGenerator: KFunction1<T, String> = ::getV2ResponseString,
) {
<Call two different endpoints for request>
val v1str = v1ResponseStringGenerator(v1Response)
val v2str = v2ResponseStringGenerator(v2Response)
<compare v1str and v2str>
}
如您所見,呼叫者可以傳入有關如何從兩個端點的回應中生成回應字串的函式。我還有回應生成器的默認功能。它們看起來像這樣:
private fun <T> getV1ResponseString(v1Response: T): String {
return v1Response.toString()
}
private fun <T> getV2ResponseString(v2Response: T): String {
return v2Response.toString()
}
這在 IntelliJ 中編譯得很好。但是,當我運行 gradle build 時,它失敗并出現錯誤
Type inference failed: Not enough information to infer parameter T in fun <T> getV1ResponseString(v1Response: T): String
Please specify it explicitly.
我究竟做錯了什么?我在我的 gradle 版本中使用 Kotlin 1.6.10。
uj5u.com熱心網友回復:
看起來這是一個已知問題,自 Kotlin 1.6.20 起已修復:https ://youtrack.jetbrains.com/issue/KT-12963 。
對于 Kotlin 1.6.10,解決方法是避免使用KFunctionN不需要的型別。例如,如果您只需要呼叫一個函式,那么只使用FunctionN型別就可以了,也用 表示(...) -> ...:
suspend fun <I, T> multiplexOrShadow(
request: I,
v1ResponseStringGenerator: (T) -> String = ::getV1ResponseString,
v2ResponseStringGenerator: (T) -> String = ::getV2ResponseString,
) {
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474119.html
