科特林宣言冲突

时间:2018-02-27 12:21:46

标签: 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

1 个答案:

答案 0 :(得分:4)

看起来没有办法,因为Kotlin生成的通用代码使用Function4作为参数类型。它是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
}