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
Joffrey
08/08/2025, 11:07 AM
Could you describe a use case for this? Why not use a map as input in the first place if you need this?
s
Starr
08/08/2025, 12:06 PM
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
jw
08/08/2025, 12:42 PM
A map also incurs boxing which changes the underlying type, at least on the JVM
s
stantronic
08/08/2025, 1:44 PM
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
Cies Breijs
08/08/2025, 11:01 PM
@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
Starr
08/09/2025, 5:40 PM
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