English 中文(简体)
pattern matching on a series of values in scala
原标题:

I m a Scala beginner and this piece of code makes me struggle.

Is there a way to do pattern matching to make sure everything i pass to Data is of the correct type? As you can see i have quite strange datatypes...

class Data (
val recipient: String, 
val templateText: String, 
val templateHtml: String, 
val blockMaps: Map[String,List[Map[String,String]]], 
templateMap: Map[String,String]
)

...

val dataParsed = JSON.parseFull(message)
dataParsed match {
 case dataParsed: Map[String, Any] => {
  def e(s: String) = dataParsed get s
  val templateText = e("template-text")
  val templateHtml = e("template-html")
  val recipient = e("email")
  val templateMap = e("data")
  val blockMaps = e("blkdata")

  val dependencies = new Data(recipient, templateText, templateHtml, blockMaps, templateMap)
  Core.inject ! dependencies
 }

...

最佳回答

I guess your problem is you want to be able to patten match the map that you get from parseFull(), but Map doesn t have an unapply.

So you could pattern match every single value, providing a default if it is not of the correct type:

val templateText: Option[String] = e("template-text") match {
  case s: String => Some(s)
  case _ => None
}

Or temporarily put all the data into some structure that can be pattern matched:

val data = (e("template-text"), e("template-html"), e("email"), e("data"),
            e("blkdata"))

val dependencies: Option[Data] = data match {
  case (templateText: String,
        templateHtml: String,
        blockMaps: Map[String,List[Map[String,String]]],
        templateMap: Map[String,String]) =>
    Some(new Data(recipient, templateText, templateHtml, blockMaps, templateMap))
  case _ => None
}
问题回答

暂无回答




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签