Is there a way in Kotlin to get all arguments of a...
# language-proposals
c
Is there a way in Kotlin to get all arguments of a function as a Map<String, Any?>, where the keys are the names of the arguments? If not would this be proposal worthy?
👀 1
j
Could you describe a use case for this? Why not use a map as input in the first place if you need this?
s
Are you referring to getting the values passed from inside the function, or getting the parameter types (ie a Map<String, Class<?>>) in a reflection context? I think the latter can be done atm A use case I can see for the former is constructing json request bodies -- which I think can be done via replacing the function with a data class with kotlinx.serialization Using a map as input in the first place would lose type safety...
j
A map also incurs boxing which changes the underlying type, at least on the JVM
s
Could this be an extension function to (the up and coming)
dataarg
classes? It could then live in kotlin.reflect and not add any overhead to modules that dont need it.
c
@Joffrey The use case being that I have an API that take a Map of "parameters" and I want to feed it the arguments. I want to add type safety by creating a function for each case. @Starr I want the values (as values) and the argument names (as keys).
s
So yeah I'd suggest making a data class instead of a function, and use kotlinx.serialization or something to convert that to a map
c
@Starr good tip! Thanks...