×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Scala
Posted by: Pahay Ragh
Added: Oct 23, 2020 2:14 PM
Modified: Oct 24, 2020 9:46 PM
Views: 4534
Tags: no tags
  1.   def typedPatternMatching(any: Any): String = {
  2.     any match {
  3.       case string: String => s"I'm a string. My value: $string"
  4.       case integer: Int   => s"I'm an integer. My value: $integer"
  5.       case double:Double  => s"$double"
  6.       case list: List[Any] if (list.size <= 10) => "Guards"
  7.       case List(0, _, _) => "a three-element list with 0 as the first element"
  8.       case List(1, _*) => "a list beginning with 1, any number of elements"
  9.       case Vector(1, _*) => "a vector starting with 1, any number of elements"
  10.       case map: Map[_, _] => map.keys.toString()
  11.       case (a, b) => s"got $a and $b"
  12.       case _ => s"I'm from an unknown type. My value: $any"
  13.     }
  14.   }