Question

I am using play2 framework, so when I retrieving parameters I always get Option. But I will only continue to process only if all the Options are matched (Not None).

I don't want to write nested match as that looks ugly.

if( isDefined("a") && isDefined("b"){
    //dosomething 
}
Was it helpful?

Solution

You can match them as a tuple

(aOpt, bOpt) match {
  case (Some(aVal), Some(bVal)) => ...
  case _ => ...
}

You could use for-comprehension syntax

for {
  aVal <- aOpt
  bVal <- bOpt
} ...

There are also some monadic combinators ((aOpt |@| bOpt) {aVal + bVal}) in the Scalaz library if you want to go that road.

Here are a similar question + answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top