Method reference or bind

I’m looking for an idiomatic way to convert object + method to function. Example:

val o = "test"
val f: () -> String = {o.toString()}

Java has method reference, so I could write o::toString, but Kotlin for some reason doesn’t have it.

Currently I’m using the following extension function:

fun <T, R> T.bind(method: T.() -> R): () -> R = {this.method()}

val o = "test"
val f: () -> String = o.bind { toString() }

but I would prefer something from a standard library. I tried to find something similar, but I couldn’t find anything.

There isn’t anything in the standard library. Bound method references will be supported in Kotlin 1.1, so it doesn’t make sense to introduce library functions for this.

1 Like