I have two methods:
class Example {
fun method(name: String): String {}
fun method(name: String, length: Int): String {}
}
And at some point in my code I need to use the second version of method
via reflection, like so:
val func = Example::method
This can't compile because the compiler can't tell the difference between the two definitions of this overloaded method. I can't find a way to specify the parameteres for the function. The documentation says:
Alternatively, you can provide the necessary context by storing the method reference in a variable with an explicitly specified type:
val predicate: (String) -> Boolean = ::isOdd // refers to isOdd(x: String)
But in my case, the function assigned to func
can be arbitrary, it does not necessarily has two parameters of types String
and Int
, and in other cases the function assigned to this property only has the single String
parameter. But in this case, I need the version with two parameters.
So, is there any way to specify which function I'm using, and if not, is there any workaround?
Aucun commentaire:
Enregistrer un commentaire