Kotlin声明冲突



我有一个问题。除了添加@JvmName外,还有其他方法可以修复此代码吗?

class Test() {
    fun <T> apply(calc: (String, List<Double>, Double, Double) -> T): T {
        return calc("a", listOf(), 1.2, 3.4)
    }
    fun <T> apply(calc: (String, Double, Double, Double) -> T): T {
        return calc("a", 1.2, 3.4, 5.6)
    }
}

上面的代码出现以下错误:

Error:(375, 9) Kotlin: Platform declaration clash: The following declarations have the same JVM signature (apply(Lkotlin/jvm/functions/Function4;)Ljava/lang/Object;):
fun <T> apply(calc: (String, Double, Double, Double) -> T): T defined in Sample.Test
fun <T> apply(calc: (String, List<Double>, Double, Double) -> T): T defined in Sample.Test

看起来没有办法,因为Kotlin生成的通用代码使用该函数4作为参数类型。它是4个通用参数的接口,因此无论类型如何,都看起来相同。

/** A function that takes 4 arguments. */
public interface Function4<in P1, in P2, in P3, in P4, out R> : Function<R> {
   /** Invokes the function with the specified arguments. */
   public operator fun invoke(p1: P1, p2: P2, p3: P3, p4: P4): R
}

最新更新