Tuesday, 18 August 2020

Scala: match expression

 ‘match’ expression similar to switch and case statement in Java, but it is more powerful than a switch-case statement.

 

Syntax

Identifier_to_be_matched match{

         case CASE_1 => expression

         case CASE_2 => expression

         ....

         ....

         case CASE_N => expression

}

 

Example

scala> var dayOfWeek = 5
var dayOfWeek: Int = 5

scala> var result = dayOfWeek match {
     |     case 1 => "Monday"
     |     case 2 => "Tuesday"
     |     case 3 => "Wednesday"
     |     case 4 => "Thursday"
     |     case 5 => "Friday"
     |     case 6 => "Saturday"
     |     case 7 => "Sunday"
     | }
var result: String = Friday

You can even keep the data associated with each case in an expression block.

scala> var result = dayOfWeek match {
     |     case 1 => {"Monday"}
     |     case 2 => {"Tuesday"}
     |     case 3 => {"Wednesday"}
     |     case 4 => {"Thursday"}
     |     case 5 => {"Friday"}
     |     case 6 => {"Saturday"}
     |     case 7 => {"Sunday"}
     | }
var result: String = Friday

What if no value matches the match expression?

Scala throws scala.MatchError.

scala> var dayOfWeek = 10
var dayOfWeek: Int = 10

scala> var result = dayOfWeek match {
     |     case 1 => {"Monday"}
     |     case 2 => {"Tuesday"}
     |     case 3 => {"Wednesday"}
     |     case 4 => {"Thursday"}
     |     case 5 => {"Friday"}
     |     case 6 => {"Saturday"}
     |     case 7 => {"Sunday"}
     | }
scala.MatchError: 10 (of class java.lang.Integer)
  ... 32 elided

Or-ed expressions

You can combine multiple case expressions using | symbol.

scala> var number = 5
var number: Int = 5

scala> var result = number match {
     | case 2 | 3 | 5 | 7 => {"Prime Number"}
     | case 9 | 11 | 13  => {"Odd Number"}
     | case  4| 6 | 8 => {"Even Number"}
     | }
var result: String = Prime Number

Pattern guards

Using pattern guards, you can add an if expression into a case.

 

Example

case someOtherNumber if someOtherNumber == 0 => {"Zero"}

 

Pattern guard starts with a placeholder variable called a value binding. In the below example, someOtherNumber is a placeholder variable.

 

Example 1

scala> var number = 23
var number: Int = 23

scala> var result = number match {
     | case 2 | 3 | 5 | 7 => {"Prime Number"}
     | case 9 | 11 | 13  => {"Odd Number"}
     | case  4| 6 | 8 => {"Even Number"}
     | case someOtherNumber if someOtherNumber == 0 => {"Zero"}
     | case someOtherNumber if someOtherNumber > 0 | someOtherNumber < 0 => {"You entered : " + someOtherNumber}
     | }
var result: String = You entered : 23

Example 2

scala> var number = 0
var number: Int = 0

scala> var result = number match {
     | case 2 | 3 | 5 | 7 => {"Prime Number"}
     | case 9 | 11 | 13  => {"Odd Number"}
     | case  4| 6 | 8 => {"Even Number"}
     | case someOtherNumber if someOtherNumber == 0 => {"Zero"}
     | case someOtherNumber if someOtherNumber > 0 | someOtherNumber < 0 => {"You entered : " + someOtherNumber}
     | }
var result: String = Zero



Previous                                                    Next                                                    Home

No comments:

Post a Comment