Array<Any> + Array<Any> = Add as element or add all?


I would have added this to the “Public Review of the Standard Library APIs”, but it’s closed.

I have been using Kotlin for a personal project, currently using the latest release candidate. While working on a custom List class I stumbled upon a rare issue, which is array concatenation involving Any elements.

Ideally, I should be able to do do array1 + array2 to concatenate them. Array's override of the + operator does one of two things: Concatenate with one element of type T, or concatenate with all elements in an Array<T>.
Array<Any> matches both Any and Array<Any>, so there is an unresolvable ambiguity in the current state of Kotlin.

I have solved the issue with an alternative approach, which was adding extension functions which imitate the functionality of the + operator overrides in the standard library that can be used without ambiguity.

Doing this left me a little unsatisfied, and I have a question. Should the + operator be able to do both single element concatenation and multi element concatenation? My only complaint is that I fear it can impact code readability in the cases where the context does not explain which one it should be doing.
I won’t deny it is very useful to have both, but I personally think it is still a questionable design decision. It could be limited to one of the functionalities.

If nothing happens with the + operator override for arrays, I believe extension functions like the ones I used should be added to the standard library.


Relates to KT-10996<Any>(array) is ambiguous for Array<Any>