Guys, is there a way to get all arguments passed t...
# announcements
g
Guys, is there a way to get all arguments passed to a function? For example something like:
Copy code
fun myFunction(arg1: Type1, arg2: Type2, ..., argn: TypeN) {
   val arguments = getAllArguments()
}
e
As far as I understand, you want to collect all your arguments of different types into a signle
List<Any>
?
b
You could work around it by using varargs for your function. Although, this won’t work if you have different types or you have to use
Any
. I would just use
listOf(arg1, arg2, etc)
. IMO there is something else wrong if you have so many arguments that this is a lot to type
g
@Egor Trutenko, yes, I would like something like these.
@Burkhard, I would like to do this with reflection, to use this on a lot of functions, to be able to log the function contents when in the debug mode of a complex system.
e
@Danilo you can get a reference to your function (as if it was an object) by prepending its name with
::
. This way, you'll be able to write something like this:
::myFunction.parameters.forEach { println(it) }
I'm not sure, though, what you'll get, you may want to experiment with it
g
@Egor Trutenko, this way I think we will get a list of arguments of the function (KProperty or something like these), but not the actual values that are being passed. Do you think it is still possible to get the actual values? Do you have something more in mind?
e
Well, Java reflection allows extracting actual values from instances. I think, Kotlin reflection might be able to do so as well
I'm trying out it now
No, I give up, I don't know. You may try annotation processing and code generation, but I doubt it would be relevant for logging purposes. JVM reflection essentially doesn't allow unifying and introspecting function parameters