Multiple return values with inline functions possible?

This is a question I have been thinking about a couple times, but my knowledge of the Kotlin compiler capabilities and the subsequent build tools (JVM, JS-Transpiler, …) is too little to find an answer. Maybe someone can shed some light on it.

The question revolves around the legendary feature of returning multiple values from a function call, which is, as it seems, not possible at the time for various reasons.

BUT … what if you’d do the following:

inline fun computePoint(whatever: SomeType): Pair<Int, Int> {
    val x = whatever.computeX()
    val y = whatever.computeY()
    return Pair(x, y)

fun callerFunction(whatever: SomeType) {
    val (x, y) = computePoint(whatever)
    // TADAAA...

Is the Kotlin compiler smart enough to optimize away the instance creation of the Pair after inlining the function? If it isn’t, can the JVM compiler or the JS transpiler do it? Or will the JVM JIT compiler optimize this at runtime?


Thanks for providing this link. If I understand this correctly, the Java Compiler is indeed able to optimize away the instance creation, but there is no guarantee.

Does anybody know whether the Kotlin compiler attempts such optimizations as well?

This document is not about the Java compiler used to compile the source code into a bytecode. It is about the JIT compilation happening in the JVM at runtime. It applies to both Java and Kotlin.


Thats how I understood it. But still thanks for clearifying. :slight_smile:

I’m still interested though if the Kotlin compiler is doing optimizations like these as well.