Hi there. Is it possible to write a function, that...
# getting-started
a
Hi there. Is it possible to write a function, that accepts a function with any number of arguments, of any kind, and execute it? The following works, but only for lambda functions
Copy code
fun <R>myFunc(block: () -> R): R
{
    println("starting)
    try
    {
        return block()
    }
    finally
    {
        println("end")
    }
}
As in the example above, I would like to do some things before the functions run, and some things after. An example would be to measure the execution time. Appreciate any help!
r
How do you expect to call the function if you don't know what to give it?
m
Here's what is in the standard library for measuring execution time. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/measure-time.html You'll notice it takes a Lambda, and returns Unit. The expectation is all your code would be in the lambda, including any function calls. As per above, there isn't a way to do what you're asking, as there's no way to know what to pass to the function.