In Java, a varargs parameter can be called using either an array or a list of values. Thus, this works:
void code() {
abc("A")
String[] array = new String[1]
abc(array)
}
void abc(String... x) { ... }
I’d like the same option to allow library users to call the method with a zero or more parameters, or with an array of parameters. So in Kotlin, I tried this code:
fun code() {
abc("A")
val array = Array<String>(1) { "B" }
abc(array)
}
fun abc(vararg x: String) { ... }
which causes a compiler error on the abc(array) call, so I added this overload to abc:
fun abc(x: Array<String>) { ... }
which fails at runtime with this ugly message:
ERROR: Platform declaration clash: The following declarations have the same JVM signature (abc([Ljava/lang/String;)V):
fun abc(x: Array<String>): Unit
fun abc(vararg x: String): Unit (75, 1)
ERROR: Platform declaration clash: The following declarations have the same JVM signature (abc([Ljava/lang/String;)V):
fun abc(x: Array<String>): Unit
fun abc(vararg x: String): Unit (79, 1)
Interestingly, Swift has the same semantics that an array cannot substitute for a varargs parameter, but they do allow the second overload and it works fine.
Any suggestions?