Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala convert Option[T] to String

Tags:

scala

Does exist any native function in scala that does the equivalent this?

def strConvert[T](v: Option[T]): String = {
    if (v.isDefined)
      v.get.toString
    else
      ""
}
like image 397
tano Avatar asked Sep 17 '15 15:09

tano


1 Answers

For generic T, you can avoid the if with map -- v.map(_.toString).getOrElse("")

like image 77
user295691 Avatar answered Sep 24 '22 14:09

user295691