https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Colton Idle

01/05/2022, 5:10 AM
I wrote two extension functions that take different Types in the List type generic, but I get an error saying that I can't do that
Copy code
private fun List<NetworkFoo>.mapToDomain(): List<DomainFoo> {
  return this.map { //stuff }
}
and
Copy code
private fun List<NetworkBar>.mapToDomain(): List<DomainBar> {
  return this.map { //stuff }
}
Error
Platform declaration clash: The following declarations have the same JVM signature
I suppose the error message is helpful in giving me a hint that these two methods are identical in JVM land? If I use the auto-correct option I get
Copy code
@JvmName("mapToDomaincom.network.model.NetworkBar")
but the annotation gives me an error of:
Illegal JVM name
e

ephemient

01/05/2022, 6:42 AM
both functions have the exact same types after erasure so they conflict. JVM name must not contain
.;[/<>
(and if targeting Android, additional restrictions apply on some versions, mainly excluding whitespace from names before Android 11)
s

Stephan Schroeder

01/05/2022, 8:09 AM
Since the return-type is different, you're probably best of using two different method names, e.g.
mapToDomainFoo
and
mapToDomainBar
. Like ephemient mentiond currently both extensions have the same jvm-bytecode representation
private static List mapToDomain(List this)
(since the JVM doesn't retain generic type info) and therefore clash.
r

Ruckus

01/05/2022, 4:00 PM
You don't need to include the package in
@JvmName
, just the function name, so
@JvmName("mapToDomainkBar")
should be what you want. I have also had issues with auto correct spewing out garbage from time to time, especially with anything related to `typealias`es.
e

ephemient

01/05/2022, 4:04 PM
if you don't have Java callers, the JVM name doesn't have to be a valid Java identifier, you could
@JvmName("map-NetworkFoo-to-DomainFoo")
and
@JvmName("map-NetworkBar-to-DomainBar")
.
c

Colton Idle

01/05/2022, 9:16 PM
Thanks for the info everyone. Learned something new. I want to just use the JvmName annotation, but if I use
Copy code
@JvmName("asdf-mapToDomain")
it still fails with the error,
Illegal JVM name
I think the kotlin compiler just got stuck in some loop. Restarted the IDE and the errors went away.
3 Views