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

Tristan

07/21/2021, 9:29 PM
Hello, Is there the possibility in Kotlin to retrieve statically the return type or parameters of a function? In TypeScript it is possible to things like, I don't know about Kotlin.
Copy code
fun getSomething(): Something {}

val something: ReturnType<typeof getSomething> = getSomething()
It may be useful for situation where:
Copy code
fun executeSomething(cb: Something.(arg: String) -> Unit) {
    val something = Something()
    something.cb("the arg")
}

// here the type could be different
// val cb: Parameters<executeSomething>[0] = {
val cb: SecretUser.(arg: String) -> Unit = {
    println(propertyOfSomething)
}

executeSomething(cb)
I am just playing with the language 😊
l

Landry Norris

07/21/2021, 9:44 PM
If you are fine with inlining the function, you could use a reified generic. If not, on JVM, the best option I know of is reflection.
t

Tristan

07/22/2021, 12:55 PM
Thank you for answering. I'll take a look at it.