Instead of using a switch statement, the “match” keyword is utilized here. The word “match” is consistently declared in the root class of Scala to ensure that all objects can access it. A pattern and one or more expressions are included in each case statement. They are only evaluated if the stated pattern matches. The arrow character (=>) is employed to differentiate the pattern from the expressions.
Components of Pattern Matching in Scala
The match expressions are composed of various components:
- “Candidate” refers to the value that we use to match the patterns.
- The “match” keyword.
- The code to run when the pattern matches, the pattern, the case keyword, and an arrow symbol make up many case clauses.
- A default statement in the absence of a pattern match. Because it contains the underscore symbol (_) and comes after the case clauses, the default clause may be easily identified.
Let’s look at a straightforward example to demonstrate those elements:
Example 1: Pattern Matching of Type “Int” in Scala
As a pattern match, an example requires an integer value and returns an integer as well. Any parameters other than integers cannot be passed. Doing so results in a failure.
Within the scala script, we established the object as “My_object” with the scala object modifier. It contains the main method that uses the println to display the statement. In the println method, we invoked the test method. The test method takes the integer type parameter “1”. Next, we have a functional approach method where we defined the test method. The test method takes the variable “i” of type “int”.
We set the “match” keyword with the variable “i”. Inside the block section, the case statements are used. We have three cases here which return the expression upon the satisfying case. The expression included in a case is evaluated if the value of “i” matches any of the cases. Since we achieved success in this first attempt, case 1 is assessed. If the value of “i” is not 0 or 1, the default case which is “case_” is carried out.
We saved the aforementioned scala program in the “example.scala” file. Then, we execute the scala program with the object name that you have given in the script. Here, we have “My_object”. We executed this object with the scala keyword. As you can see, the case 1 expression is obtained because it matched the pattern.
Example 2: Pattern Matching of Type “String” in Scala
Each object in Scala has a static type that cannot be modified because it is a typed language. An object of the type String, for instance, can only have the String expressions. As seen in the following illustration, scala makes it simple to compare the objects to type patterns.
Begin by constructing the object of the previous scala script. The object is assigned the name “linux_object”. Inside the body of that object, we have the main function definition. The println method is invoked within the main function definition where we called the test method and passed the string parameter “saeed” to it. After that, we have a test function where the variable “x” is declared of the type string.
We assigned the match modifier to this variable “x” for a matching pattern of the cases. Within the block of the test method, three cases are provided with the expressions. Here, the case with the wildcard “_” is executed because no pattern is matched with the “x” sequence.
The scala script is compiled with the scala modifier and the name of the object “linux_object” from the previous program. The output obtained has the expression “Default case Executed” from the “case_”.
Example 3: Pattern Matching of Case Classes in Scala
Case classes are unique classes that are utilized in the case expression-based pattern matching. These are conventional classes with the special modifier case. The example that follows is a simple illustration of pattern matching that uses the case class:
By defining the “My_Demo” object, we started this scala script. Inside the “My_Demo” object body, we have a main method definition. Here, we declared the three variables – “cyan”, “dora” and “marrie” – with the keyword “val”. The variables called the “Person” class with the new keyword and set the values against each variable. The values passed to the “Person” class are the string values and the integer value. Then, we iterate through the list of the “cyan”, “dora”, and “marrie” variables using the for loop and utilize the case expressions with the “Person” method constructor by giving the values.
The “Cyan” variable is the matching expression in the first case, the name “Dora” is the matching expression in the second case, and for any other values, the details are shown as provided in the last case. The p_name and the p_age parameters are given to the Person constructor for an empty one. Last but not the least, the class person is empty because no method has to be defined inside of it.
When the previous scala script is executed, it displays all the case’s pattern matching expressions.
This is a manual for Scala Pattern Matching. Pattern matching in Scala is improved than switching in Java. The match keyword is used to match the input that has been provided in Scala. Here, we go over a brief introduction to Scala’s pattern matching functionality, along with the proper syntax and relevant examples. Pattern matching has the benefit of being both incredibly versatile and extremely powerful. The data structure is dynamically destructible within the pattern. The variables that are specific to this expression can then be allocated directly to those components.