Today I found out that JVM is capable of calling t...
# announcements
r
Today I found out that JVM is capable of calling the correct overload of a method only based on the distinguishing return type. That's awesome 👍😁, especially if you want to deprecate extension functions. For instance:
Copy code
class A{}
fun A.test(i: Int): Int = 1
@Deprecated("use new fun which returns int")
fun test(a: A, i :Int): Float = 1.1f

fun main(vararg args: String){
    val a = A()
    a.test(1)
    test(a, 1)
}
a
JVM is capable of calling the correct overload of a method only based on the distinguishing return type
no, not really
r
it does, try out the above
a
it's not because of return type, it's because of the extension function
try it with normal functions
also try calling the above
test
functions from java
r
I think you mix up JVM with Kotlin, JVM does not have extension functions.
and you are right, Kotlin does not support method overloading based on return type and neither does Java
a
I think you mix up JVM with Kotlin, JVM does not have extension functions.
extension functions defined in kotlin are accessible in java as static methods with the receiver as first parameter
All the functions and properties declared in a file
example.kt
inside a package
org.foo.bar
, including extension functions, are compiled into static methods of a Java class named
org.foo.bar.ExampleKt
.
https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#package-level-functions
r
exactly, JVM does not have extension functions that's why they are transformed to static methods.
or what do you want to say with the links?
a
didn't your remark refer to me saying "try calling above
test
functions from java"?
r
ah no... sorry, try calling it from kotlin 🙂 and you are right that it only works in kotlin due to the extension function vs. normal function trick
I am not aware of any language on the JVM which is able to call a method based on the return type, but there might be a dynamic one which is capable of doing it in the language itself