Take the idea of a simple case-switch and put it on Steroids. Sure you can do basic stuff like check user input and such. But with the power of Scala you can take it a step further and it can decompose the value into separate parts and then bind them to variables.
The keyword match checks ch against the case statements and sets values. Notice the _ that match hits if nothing else matches above. It is there to ensure a value is set for the cases.
You can match a class type and then invoke specific behavior on the instance. With a few lines of code you can fluidly control complex behaviors using this technique.
With Case Classes
These are classes that work specifically well with Scala’s Pattern Matching mechanism.
Pay close attention to Appliance with the code above we can now do:
Using this class wrapper around some functionality and using pattern matching is powerful and solves many syntax issues in one fell swoop.
With Pattern Guards
In case the matching is too generic you can add guards to narrow down the matching. Guards are boolen checks that are added following the matched case.