Matching a tuple using a comparison operator

I would like a match with a tuple pattern, but I cannot find any solution for matching using comparison operators. My code is:

myTuple  match {       
      case (-1,-1,true) => ...       
      case (_>=0,-1,_) =>  ...
    }

This gives a compile time error. I also tried to use if the guard, but, as I see, it can not be used as follows:

 case (_ if _>=0,-1,_) =>  ...

Is my approach right or should I solve it differently? thanks Zoltan

+5
source share
1 answer

The syntax is incorrect, you should use protection as follows:

myTuple  match {       
  case (-1,-1,true) => ...
  case (x,-1,_) if x >= 0 =>  ...
  case _ => ... // default
}

There are many good examples for matching the scala pattern on the Internet. Here is the first detailed one I found on google: Playing with scala pattern matching

+9
source

All Articles