Last active
November 4, 2020 01:34
-
-
Save rupeshtr78/8c9abdff9d7396e195cb9b27fd010575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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