Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 544
Jayson Minard Avatar asked Feb 23 '16 14:02

Jayson Minard


People also ask

Can you mention some of the extension methods used in Kotlin?

The list object call the extension function (MutableList<Int>. swap(index1: Int, index2: Int):MutableList<Int>) using list. swap(0,2) function call. The swap(0,2) function pass the index value of list inside MutableList<Int>.

Which of the following extension method are used in Kotlin?

Kotlin with top-level extension function is a pure Java class with a static method. That's why it needs to somehow pass the original String.

Which feature of Kotlin helps to extend a class with new functionality without having to inherit from the class?

Kotlin provides the ability to extend a class or an interface with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions.

How do you think extension functions in Kotlin are useful?

Extension functions are a cool Kotlin feature that help you develop Android apps. They provide the ability to add new functionality to classes without having to inherit from them or to use design patterns like Decorator. Read more about Decorator pattern on Wikipedia.


1 Answers

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()
like image 198
Jayson Minard Avatar answered Oct 13 '22 20:10

Jayson Minard