Scala pattern matching OR

Let’s continue the pattern matching exploration.

How would you match several conditions at once ? We are going to learn this today.

Once you know the syntax, it is pretty straight forward, you have to use the symbol |. Like the boolean operator OR: ||.

The syntax is:

case valueName @ ( enum1 | enum2 | enum3 ) => ...

You can also do the same thing without the @:

case ( enum1 | enum2 | enum3 ) => ...

And you can do the same thing with case class:

case valueName @ ( CaseClass(_, exactMatch) | CaseClass(match2, _) ) => ...

Next time, we are going to dive more into the case class use case.

Reveal more information and clues
Load Exercise