https://kotlinlang.org logo
#announcements
Title
# announcements
f

Fré Dumazy

10/06/2017, 8:10 AM
Hi, I'm having some problems with generics: How do I create a method that accepts
<T: MyObject> myMethod(arg: MyWrapper<T>)
and
<T: MyObject> myMethod(arg: MyWrapper<Array<T>>)
If I try this in 2 methods with the same name, it says "The following declarations have the same JVM signature"
d

diesieben07

10/06/2017, 8:12 AM
It's a platform limitation, you can work around it by annotating one method with
@JvmName
and provide an alternative platform name to use.
👍 1
f

Fré Dumazy

10/06/2017, 8:17 AM
oh, okay. Thanks, it works 🙂
k

karelpeeters

10/06/2017, 8:17 AM
The problem behind this is that generics are erased, they don't exist at runtime, so both methods would have the same signature.
f

Fré Dumazy

10/06/2017, 8:21 AM
Hmm, interesting. I'm reading kotlin in action, but haven't got to the generics part yet. Hopefully it will explain this issue and give an idea of the inner workings
g

gildor

10/06/2017, 8:23 AM
@Fré Dumazy It’s because of generics implementation on JVM, you can find a lot of explanation if google “java type erasure”.
👍 1
m

mannodermaus

10/06/2017, 8:28 AM
That
@JvmName
approach to getting around the limitation is actually pretty genius and handy, esp. if you don’t have to interop