8

Let's say I have:

inline class Email(value: String)

and

fun something(email: Email)

now if I want to call something() from Java I can't. Because any method that is accepting an inline class as a parameter is "mangled" (more about this here: https://kotlinlang.org/docs/reference/inline-classes.html#mangling) As far as I understood mangling renames the method name to include a "-" that's an invalid character in Java, so the method is practically invisible from Java perspective. That's intentional probably because of strong type safety. But I really need to make the method callable from both Java and Kotlin. Do you know some workaround?

WindRider
  • 11,958
  • 6
  • 50
  • 57

2 Answers2

7

According to the KEEP that is used to discuss and plan inline classes, this is not currently possible (writing as of 1.3.11):

We'll compile function compute(UInt) to compile-<hash>(Int), where <hash> is a mangling suffix for the signature. Now it will not possible to call this function from Java because - is an illegal symbol there, but from Kotlin point of view it's a usual function with the name compute. [Emphasis mine]

Keep in mind that inline classes are experimental and subject to change, so perhaps in a future release this will be possible. I did try annotating this with @JvmName and that is also not supported.

Todd
  • 30,472
  • 11
  • 81
  • 89
  • 1
    I was wondering this myself for a while; +1 for reference. Hopefully they do support this at some point (perhaps by replacing the inline class by the type that it wraps), as the tradeoff between JVM interop and performance/idiomaticity/convenience is unfortunate. – Salem Jan 17 '19 at 16:45
  • 1
    @Moira - I agree it would be nice. I feel that given the experimental nature of it, that there will be changes to make it better. – Todd Jan 17 '19 at 17:00
  • 1
    Guys, write one empty Kotlin function taking an online parameter, go to Tools > Kotlin > Show bytecode. Then click Decompile button. You'll see exactly how the Java world sees it. The problem is the invalid mangled name not that type is not unwrapped. It is unwrapped. – WindRider Jan 17 '19 at 19:06
  • @WindRider - Yes, that's exactly what the part I quoted said. It's all about how Kotlin choses to mangle the name so it is valid in Kotlin but not Java. – Todd Jan 17 '19 at 19:08
  • Is there a youtrack ticket to vote for such a feature? – Holger Brandl Sep 08 '21 at 13:39
1

Please, manually disable Kotlin names mangling with @JvmName

@JvmName("something")    
fun something(email: Email)

See docs for more details

sanya5791
  • 433
  • 4
  • 5