0

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 
}
0__
  • 66,707
  • 21
  • 171
  • 266
zinking
  • 5,561
  • 5
  • 49
  • 81

1 Answers1

6

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.

Community
  • 1
  • 1
0__
  • 66,707
  • 21
  • 171
  • 266