Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Last active November 4, 2020 01:34
Show Gist options
  • Save rupeshtr78/8c9abdff9d7396e195cb9b27fd010575 to your computer and use it in GitHub Desktop.
Save rupeshtr78/8c9abdff9d7396e195cb9b27fd010575 to your computer and use it in GitHub Desktop.
def typedPatternMatching(any: Any): String = {
any match {
case string: String => s"I'm a string. My value: $string"
case integer: Int => s"I'm an integer. My value: $integer"
case double:Double => s"$double"
case list: List[Any] if (list.size <= 10) => "Guards"
case List(0, _, _) => "a three-element list with 0 as the first element"
case List(1, _*) => "a list beginning with 1, any number of elements"
case Vector(1, _*) => "a vector starting with 1, any number of elements"
case map: Map[_, _] => map.keys.toString()
case (a, b) => s"got $a and $b"
case _ => s"I'm from an unknown type. My value: $any"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment