Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading