Issue
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?
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.
Answered By - Sweeper
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.