Skip to content

Instantly share code, notes, and snippets.

@MaxwellBo
Last active September 30, 2019 08:33
Show Gist options
  • Save MaxwellBo/92567ce6f325057e5bf31ba45bff8a20 to your computer and use it in GitHub Desktop.
Save MaxwellBo/92567ce6f325057e5bf31ba45bff8a20 to your computer and use it in GitHub Desktop.
val Pattern = "([a-cA-C])".r
def matchExample(x: AnyVal): Unit = {
x match {
case i: Int => "Matching on type"
case 1 => "Matching on a literal"
case 2 | 3 => "Matching on multiple literals"
case x if 0 until 10 contains x => "Matching with guard"
case ab@(a, b) => "Matching and destructuring a tuple, but keeping the original tuple bound to ab"
case x::xs => "Matching and destructuring a list"
case Pattern(c) => "c bound to capture group here (if x were a string). This will fail if the Regex doesn't match"
case _ => "Matching wildcard"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment