Scala match question
- by javier
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