Warm tip: This article is reproduced from serverfault.com, please click

inumtable.List[Object] found required List[Contact]

发布于 2020-11-28 15:34:41

I'm having some problems with the following code:

object GestorContactos {
  private var listaContantocs:List[Contacto] = Nil
  def insertarOrd(c:Contacto):List[Contacto]=listaContantocs={
    var aux:List[Contacto]=listaContantocs.sortBy(_.edad)
    aux.filter(x=>x.edad<c.edad)::c::(aux.filter(x=>x.edad>c.edad))
  }

aux.filter(x=>x.edad<c.edad)returns List[Contacto] and the same does aux.filter(x=>x.edad>c.edad).

The problem is that when I try to add both results with a Contacto Object (var c) on a single list the following error appears:

type mismatch;
 found   : List[Contacto]
 required: Contacto

It seems that listaContantocs, a inmutable List[Contacto], does not like the return type List[Object]. Is there any way I could cast List[Object] to List[Contacto]?

Questioner
ignacio aranguren
Viewed
0
Ivan Stanislavciuc 2020-11-28 23:42:50

You are combing different element types List[Contacto] and Contacto with operator ::.

The following is what you want

aux.filter(x=>x.edad<c.edad) ++ (c :: (aux.filter(x=>x.edad>c.edad)))