This question is about assign parameter value Option[List[String]]
in Scala.
My code:
def mapListString(pList : Option[List[String]]) = pList match {
case None => "-"
case Some(cocok) => cocok
case _ => "?"
}
A List
is immutable in Scala. You cannot append/prepand values to the same list. If you do, you'll be given a new List
containing the added values.
One possible implementation would look like this:
scala> :paste
// Entering paste mode (ctrl-D to finish)
val pList: Option[List[String]] = Some(List("hello"))
val result = pList match {
case None => Some(List("-"))
case Some(cocok) => Some("x" :: cocok)
}
// Exiting paste mode, now interpreting.
pList: Option[List[String]] = Some(List(hello))
result: Some[List[String]] = Some(List(hello, x))
Scala favors immutability and provides you convienient syntax to work with them. However, mutable lists do exist in Scala, you can find one, e.g, in scala.collection.mutable.MutableList