Kotlin thinks that two methods have the same JVM s

2019-07-14 13:56发布

问题:

I came from the C# background and I know how to implement this in C#, but I'm struggling with Kotlin. I've got 2 extension functions:

fun <T> Foo<T>.myFunction(func: () -> Unit): Foo<T>

and

fun <T> Foo<T>.myFunction(func: () -> Foo<T>): Foo<T>

Clearly, the return type of func is different in both functions. The first function executes it and returns this, the second executes func and returns the result of func. But it gives me an error:

"Platform declaration clash: The following declarations have the same JVM signature".

How to implement this correctly in Kotlin?

回答1:

On the JVM, we have to contend with type erasure. Meaning essentially that the types (T in this case) are thrown away in the compiled bytecode and that required checks are only done at compile time. Given that, you have to look at your function declaration with that in mind.

Kotlin will define your function argument as a Function0 in both cases. Because the types are erased, () -> Unit and () -> Foo<T> both look the same in the bytecode. We can prove this out by decompiling the code you've provided (I renamed one of these myFunction2 to get this to work):

public final class com/ginsberg/KotlinStuffKt {
    public final static myFunction(Lcom/ginsberg/Foo;Lkotlin/jvm/functions/Function0;)Lcom/ginsberg/Foo;

    public final static myFunction2(Lcom/ginsberg/Foo;Lkotlin/jvm/functions/Function0;)Lcom/ginsberg/Foo;

}

This is what the Kotlin compiler is generating (it does more, but I have removed the non-essential parts from this example). As you can see, our types are gone thanks to type erasure. And if we undo my change (myFunction2 becomes myFunction), there's no way at all to tell these apart. This is what the compiler is complaining about - if you erase the types the JVM can't tell these functions apart.



回答2:

Your functions have a conflicting signature within the JVM due to type erasure (the internal Function0<T> class being used to represent the function parameters); and you can fix this by giving each of them a JVM specific name. From Kotlin you would still access them by the original name, but from Java or internally another name is actually used. Simply use the @JvmName annotation on the alternative versions:

fun <T> Foo<T>.myFunction(func: () -> Unit): Foo<T>

@JvmName("myfunctionWithFoo")   
fun <T> Foo<T>.myFunction(func: () -> Foo<T>): Foo<T>


标签: kotlin