Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!