So, I have some Java methods with these signatures (removed annotations and code body for the sake of simplicity):
public class JavaClass {
public static <E extends CharSequence> E join(E... array) { ... }
public static <E extends CharSequence> E join(CharSequence separator, E... array) { ... }
}
And I have some code in Kotlin, which calls the 'join' method:
class KtClass {
fun test(vararg array: String) {
JavaClass.join(*array)
}
}
So far so good; it will spread the varargs and call the former method signature. Okie dokie!
The problem comes if, for example, I want to call the latter method signature, with a 'separator' argument:
class KtClass {
fun test(vararg array: String) {
JavaClass.join("<br>", *array)
}
}
This code won't compile. The compiler can't get to decide which method to call. The error:
Error:(5, 13) Kotlin: Cannot choose among the following candidates without completing type inference: public open fun join(vararg array: String!): String! defined in JavaClass public open fun join(separator: CharSequence!, vararg array: String!): String! defined in JavaClass
I can't even name the arguments because Kotlin doesn't let argument naming for non-Kotlin functions.
EDIT: Replaced the E generic type parameter with plain String references in the Java method headers, and it worked! So I guess this to be an incompatibility of type inference with generic types or something like that?
I'm pretty sure this has to be something with the spread operator (*). But I can't pass the varargs parameter array
to the join
function if I don't use it.
How can I solve this without touching the Java code?
YES, I KNOW that there is Array.joinToString extension function, but this would solve only this particular case. I need to know a generic solution.
I don't think that's Kotlin specific. The problem is that the generic argument,
E
is of typeCharSequence
so your call becomes something likejoin("separator", "word1", "word2")
which is, indeed, ambiguous since the first argument, of typeE == CharSequence
is the same as the type of the other args.