Hi, I'm having some problems with generics: How do...
# announcements
f
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
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
oh, okay. Thanks, it works 🙂
k
The problem behind this is that generics are erased, they don't exist at runtime, so both methods would have the same signature.
f
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
@Fré Dumazy It’s because of generics implementation on JVM, you can find a lot of explanation if google “java type erasure”.
đź‘Ť 1
m
That
@JvmName
approach to getting around the limitation is actually pretty genius and handy, esp. if you don’t have to interop