Scala match question

Posted by javier on Stack Overflow See other posts from Stack Overflow or by javier
Published on 2010-06-15T22:48:51Z Indexed on 2010/06/15 22:52 UTC
Read the original article Hit count: 328

Filed under:
|

Hello to everyone. I came across with an error on my Scala code that I cannot solve by myself (I am new at Scala). I have the following code:

def myFunction(list: List[Any]): String = {
  var strItems : String = "";
  list.foreach(item => {
    strItems += item match {
      case x:JsonSerializable => x.toJson()
      case y:String => ("\"" + y + "\"")
      case _ => item.toString
    }
    if(item != list.last)
      strItems +=  ",";
  })
  strItems;
}

The error I am getting is:

error: pattern type is incompatible with expected type; found : String required: Unit case y:String => ("\"" + y + "\"")

Any idea why?

PS: is there a more performant way to code myFunction

© Stack Overflow or respective owner

Related posts about scala

Related posts about match