I have two Kotlin extension methods for the same class, but with a different generic signatures and the compiler complains

一世执手 提交于 2019-12-03 07:11:59

问题


I am writing two extension functions for the same class:

class Something<T:Any> { ... }

They look like:

fun Something<Int>.toJson(): String = ...
fun Something<Double>.toJson(): String = ...

And results in the compiler error:

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

How can I create two extension functions with only the generics signature differing? or is it not possible?

Note: this question is intentionally written and answered by the author (Self-Answered Questions), so that the answers to commonly asked Kotlin topics are present in SO. It originated in Kotlin slack #general channel.


回答1:


Kotlin has the @JvmName annotation specifically for this type of use case. In Kotlin, there isn't a problem because it knows the difference between the methods. But the Java compatible byte code would have a conflict for naming since the generics erased signatures would be identical.

Therefore you need to use this annotation to control the name from the perspective of Java and the JVM. Your Kotlin code will not see this alternative name and will use the name as you intended.

Change your code to:

@JvmName("somethingIntToJson") fun Something<Int>.toJson(): String = ...
@JvmName("somethingDoubleToJson") fun Something<Double>.toJson(): String = ...

From Kotlin, use normally:

val someIntyThing = Something<Int>(194)
val json = someIntyThing.toJson()


来源:https://stackoverflow.com/questions/35579828/i-have-two-kotlin-extension-methods-for-the-same-class-but-with-a-different-gen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!