Oct
26
Match Expression in Scala
Hey there! Welcome to ClearUrDoubt.com. In this post, we will look at a program to demonstrate the Match Expression in Scala. Match Expression is used to implement Java switch statement flow in Scala. Let’s look at the sample program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.clearurdoubt object MatchExpressionDemo { def main(args: Array[String]) { matchExpression(10) matchExpression(10.0) matchExpression("10.0") matchExpression('C') } def matchExpression(ex: Any) = ex match { case a: Double => println(s"$a is a Double.") case b: Int => println(s"$b is a Integer.") case c: String => println(s"$c is a String.") case _ => println(s"$ex is something else.") } } |
Output: Read more