Frage

could you help me concerning a basic question? I have a list of "Rdv" (meetings) where Rdv is a case class having 3 fields storing phone numbers as Strings: telBureau, telPortable & TelPrivé.

I get this list from slick, via a native SQL query; this query fills the 3 phone number fields with either a String or "null"(a null object, not the "null" string). I would like to remove these null fields, so I wrote this:

var l2:List[Rdv] = liste.list()
l2=l2.map( (w:Rdv) =>{
    if ( w.telPrivé==null ) w.copy( telPrivé = "" )
})

but I get this error:

found:List[Any], required:List[Rdv]

so after the map I added ".asInstanceOf[List[Rdv]]" but then I get this error:

java.lang.ClassCastException: scala.runtime.BoxedUnit cannot be cast to metier.Objets$Rdv

It seems to ba a basic question, but I can't do that.

olivier.

War es hilfreich?

Lösung 2

Could you try doing something like this?

val l2: List[Rdv] = liste list ()
val l3 = ls map{
  case x @ Rdv(_, null, _) => x.copy(telPrive = "")
  case x => x
}

Honestly, should should make that field, if it's nullable to be an Option and then have a member function which you call defined such that:

case class Rdv(a: String, b: Option[String], c: String){
  def realC = b getOrElse ""
}

Andere Tipps

Try this:

var l2: List[Rdv] = liste.list()

l2 = l2 map ((w: Rdv => if (w.telPrivé == null) w.copy( telPrivé = "" ) else w)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top