Is there any way of wrapping arbitrary functions? ...
# getting-started
Is there any way of wrapping arbitrary functions? I want do something like
Copy code
class FunctionWrapper<T: Function<Unit>>(val fn: T){
  fun invokeWithLogging(some_args: T.Arguments) {
    // some logging here
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?
Would the logging need to involve the function's arguments? If not, here's a
type I wrote a while back that allows you to wrap arbitrary functions in a type-safe manner, with the caveat that its
functions have to be written for every arity.;cid=C0B8MA7FA If you need the function's arguments, it's trivial to modify the code to include passing an
that has the function's arguments to the
. Note that this is likely to impact performance (due to array creation and boxing etc)
ah, that's very helpful - thanks! (no, we don't need the function arguments for this use case, but good to have a strategy for that if we need it)
The code should be very extensible to allow any use-case, really. You'll just have to write (or likely generate with a simple string template) the
an approach like would also work for logging function parameters
a compiler plugin is definitely overkill for most purposes though, I wouldn't do that if you have other options
(if targeting jvm/android only, I'd choose to perform bytecode manipulation like instead of a compiler plugin since it's more stable, but either way, if you can do it in plain code it's much more straightforward)
If you're Kotlin code backend is the JVM you could fallback to using Java libs: though it'd be to heavy of an approach for me (should you use Spring, then AspectJ is already included though).