Wai-Wai Ng
06/07/2023, 4:54 PMclass FunctionWrapper<T: Function<Unit>>(val fn: T){
fun invokeWithLogging(some_args: T.Arguments) {
// some logging here
fn.invoke(some_args)
}
}
But that's not valid Kotlin before I don't think you can get the type of a generic function's arguments, and even less declare/invoke functions in that matter. Any thoughts?Youssef Shoaib [MOD]
06/07/2023, 4:59 PMWrapper
type I wrote a while back that allows you to wrap arbitrary functions in a type-safe manner, with the caveat that its wrappedWith
functions have to be written for every arity.
https://kotlinlang.slack.com/archives/C0B8MA7FA/p1679698518922679?thread_ts=1679696808.337589&cid=C0B8MA7FA
If you need the function's arguments, it's trivial to modify the code to include passing an Array<Any?>
that has the function's arguments to the wrapper
. Note that this is likely to impact performance (due to array creation and boxing etc)Wai-Wai Ng
06/07/2023, 5:02 PMYoussef Shoaib [MOD]
06/07/2023, 5:06 PMwrappedWith
functions.ephemient
06/07/2023, 6:19 PMephemient
06/07/2023, 6:19 PMephemient
06/07/2023, 6:23 PMStephan Schröder
06/08/2023, 8:48 AM