Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda