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?