Hi :wave: (@here) What is the ideal way to get ho...
# announcements
s
Hi 👋 (@here) What is the ideal way to get hooks for method execution such as before & after to use with delegate for decorator pattern to perform some action before & after of method execution without using reflection to work with graalvm
r
If you don't want to use reflection, then I guess inline function taking a lambda as parameter would be an option?
Copy code
inline fun <T> decorator(crossinline block: () -> T): T {
    println("Before")
    val result = block()
    println("After, returned $result")

    return result
}

fun decorated() = decorator {
    println("function!")
    4
}

decorated()
eg smth like this would return 4 and print
Copy code
Before
function!
After, returned 4
(crossinline stops it from being able to cut the "after" part of decorator with return)
s
thank you so much @Roukanken , does crossline also works in delegate for example I want to decorate datasource
a
Actually crossinline is not needed here. It is only required if the block has to be used somewhere else (inside another scope). It only instructs compiler that you cannot return from the outer function i.e.
decorated()
here.
Unfortunately, for delegation, no. We can have a ir compiler plugin to implement that, but I guess its still better to make it available from the stdlib and maybe we can discuss it in #language-proposals or keep (or youtrack).