P1NHE4D
P1NHE4D

Reputation: 1371

Why does Kotlin change a list of type List<List<Int>> to List<Any> if a list is added with the "+" operator?

Given the following two lists

val listA = listOf<List<Int>>()
val listB = listOf(1, 2, 3)

and the following operation

val listC = listA + listB

I am adding a list with the type List<Int> to a list with the type List<List<Int>>. However, my IDE is telling me that the type of the resulting list C is List<Any> and not, as I expected, List<List<Int>>. Could someone explain to me why?

Upvotes: 2

Views: 293

Answers (1)

Sweeper
Sweeper

Reputation: 271300

Have a look at the list of all plus operators in kotlin.collections, and you'll see that the one you are calling resolves to this one:

operator fun <T> Collection<T>.plus(
    elements: Iterable<T>
): List<T>

The compiler tries very hard to infer a type for T, so that the call is valid, and it finds Any as such a type. After all, a List<List<Int>> is a Collection<Any> and a List<Int> is also a Iterable<Any>.

Note that there is also:

operator fun <T> Collection<T>.plus(element: T): List<T>

However, overload resolution doesn't pick this one because the one that takes a Iterable<T> is considered "more specific". After all, it takes specifically an Iterable, rather than just any type (T), including Iterable. See also the rationale section of the spec.

You should probably use plusElement instead.

Upvotes: 4

Related Questions