How to convert a function to a value?


#1

Kotlin code:

``

fun hello(who: String, message: String) {
  println(who + ", " + message)
}

fun test(func: (String, String) -> Unit) {
  func(“Kotlin”, “hello”)
}

fun main() {
  test({(a, b) -> hello(a, b) })
}

You can see the last line is pretty complex. Is there any way to write it simpler?

e.g. in scala, we can write:

test(hello _)

#2

You can use method references here, but they're currently work in progress and are implemented in JVM only:

``

fun main() {
  test(::hello)
}