Lambda type with unknown number of arguments

I created a map that will store functions that will be called later, its type is as follows:

Map<String, () -> Unit>

      

The point is, I don't know how many parameters each function stores, or the types of those parameters.

Currently, the map type Map<String, () -> Unit>

allows me to store functions that have no parameters and the return type is Unit.

How can I imagine a function type that would allow me to store functions with an unknown number of parameters and an unknown return type, but it still has to be the function that is being stored.

Something like this is what I'm looking for:

Map<String, (T, X, Y, ...) -> V>

      

How can I represent this type of function in Kotlin?

+3


source to share


2 answers


All functional types in Kotlin implement Function

( https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-function.html ) so you can use it or a subclass of it



+4


source


You can pass parameters in a list:



Map<String, (List<Any>) -> Unit>

      

+2


source







All Articles