How to declare parameter as function without return value?

Viewed 4503

I have a function in kotlin

fun printExecutionTime(block: () -> Any) {
    run {
        val currentTimeMillis = System.currentTimeMillis()
        block()
        Logr.d("Execution time of " + block.javaClass.name + " -> " + System.currentTimeMillis().minus(currentTimeMillis))
    }
}

In java code, I want to pass void reference function as parameter, but can't bcs of return value

PerformanceKt.printExecutionTime(this::voidFunc);

One way will be to use interface

interface Action {
    fun call()
}

Is it possible to declare it in kotlin without an extra interface, so the code above will work?

3 Answers

I would use a Runnable or a Consumer if i had to use that function in java code too (otherwise i would use Unit as pointed by other anwers)

fun doIt(person:Consumer<String>, action: Runnable){
    person.accept("A message")
    action.run();
}
Related