Updated April 12, 2023
Introduction to Scala Pattern Matching
In scala pattern matching, we match a specific pattern in the sequence of string or an input parameter passed. It works in the same like a switch statement in java but the keyword is different here. In Scala we use match keyword to check the sequence, so we can say it is a technique to check the specified pattern against the input parameter passed.
Syntax
def method_name(variable_name:data_type): return_type = variable_passes match {
case 0 => "your_output1"
case 1 => "your_output1"
case _ => "your_output1"
}
In the above syntax, we define our method with the data_type and return type and match the keyword. Inside it, we are defining our own cases to get the desired output. If the variable passed matches with the case then the particular block of code will get executed.
We can see practice syntax below for better understanding;
def dmeo(s:String): String = s match {
case A => "hello"
case B => "bye"
case C => "world"
case _ => "not now.!!"
}
If the variable passed matched then the message will be printed. This arrow symbol is used to separate the message and the pattern.
How does Pattern Matching work in Scala?
Pattern Matching in scala in the enhanced version of the switch statement in java. It matches a particular pattern against value passed. We must have at least one case inside the match.
Let’s take one example to understand its working in scala.
Code:
object Demo {
def main(args: Array[String]) {
println(matchDemo("D"))
println(matchDemo(1))
println(matchDemo("Z"))
println(matchDemo("B"))
}
def matchDemo(obj: Any): Any = obj match {
case "A" => "first case executed"
case 1 => "second case executed"
case "D" => "third case executed"
case "Z" => "fourth case executed"
case 10 => "fourth case executed"
}
}
In the above example, we make one method name match Demo which is taking a variable of any type that can be string, integer, float, etc. If the value passed matched with the case statement then the particular statement will be executed. We are using the arrow operator to separate the message and the pattern. it works as a callback here.
In this our match function should contain one case statement at least otherwise it will generate an error. This match keyword is defined in the root class of scala so it can be available to all other classes.
Some points we should keep in mind while working with a matching pattern which is mentioned below;
- In the pattern matching we have one default case statement as well “_” is this represent like this. If we pass any pattern to the match keyword and if it does not get matched with any case mentioned then this default case will get executed. This is also called as “catch-all cases” in scala.
- In scala pattern matching we should have at least one case.
- We can also have multiple patterns matching for this we can use pipeline “|”.
- Our pattern-matching function should always return some value. It can be anything.
- Like in java we have a break statement but in scala, pattern matching does not have a break statement.
- In matching function case should contain some pattern, not a statement.
Examples to Implement Scala Pattern Matching
Below are the examples mentioned:
Example #1
An example will take an integer value as a pattern matching and also it will return an integer. We cannot pass parameters other than integer it will give an error.
Code:
println( "pattern match output for 1 is :::" +matchDemo(1))
println( "pattern match output for 1 is :::" +matchDemo(100))
println( "pattern match output for 1 is :::" +matchDemo(3))
println( "pattern match output for 1 is :::" +matchDemo(9))
// match function return integer
def matchDemo(x: Int): Int= x match {
case 1 => 100
case 2 => 200
case 4 => 300
case 9 => 400
case 10 => 500
case _ => 1000
}
}
Output:
Example #2
This example takes the value as a string data type only.
Code:
println( "pattern match output for a is :::" +matchDemo("A"))
println( "pattern match output for p is :::" +matchDemo("P"))
println( "pattern match output for o is :::" +matchDemo("O"))
println( "pattern match output for s is :::" +matchDemo("S"))
// match function return Stirng
def matchDemo(x: String): String= x match {
case "A" => "Hello world"
case "C" => "bye world"
case "Z" => "not now world"
case "P" => "hii"
case "O" => "happy"
case _ => "No case matched!"
}
}
Output:
Example #3
In this example, we can pass any type of pattern.
Code:
println( "pattern match output for a is :::" +matchDemo("A"))
println( "pattern match output for p is :::" +matchDemo("P"))
println( "pattern match output for z is :::" +matchDemo("O"))
println( "pattern match output for s is :::" +matchDemo("S"))
println( "pattern match output for 001 is :::" +matchDemo(001))
println( "pattern match output for 009 is :::" +matchDemo(009))
// match function return Any
def matchDemo(x: Any): Any= x match {
case "A" => "Hello world"
case "C" => "bye world"
case "Z" => "not now world"
case "P" => "hii"
case 001 => 200
case 002 => 003
case 009 => 900
case _ => "No case matched!"
}
}
Output:
Type of Pattern Matching in Scala |
Tuple pattern | Extractor pattern | Sequence pattern | XML pattern | Regular Expression Pattern |
Typed pattern | Pattern binders | Literal pattern | Pattern alternatives | Infix operation pattern |
Variable pattern | Stable identifier pattern | Constructor pattern | Irrefutable Pattern |
Conclusion
Scala pattern matching is more enhance then switch in Java. Scala, we use match keyword to match our input passed. It should contain the return type and at least one case inside to match. It also declares in the root class of scala to make it available for other classes.
Recommended Articles
We hope that this EDUCBA information on “Scala Pattern Matching” was beneficial to you. You can view EDUCBA’s recommended articles for more information.