Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Created January 7, 2012 22:35
Show Gist options
  • Select an option

  • Save diegopacheco/1576302 to your computer and use it in GitHub Desktop.

Select an option

Save diegopacheco/1576302 to your computer and use it in GitHub Desktop.
Scala Pattern Matcher Advanced
trait Color
case class Red(saturation: Int) extends Color
case class Green(saturation: Int) extends Color
case class Blue(saturation: Int) extends Color
def matcher(arg:Any): String = arg match {
case "Chowder" => "Make with clams"
case x: Int => "An Int with value " + x
case Red(100) => "Red sat 100"
case Green(s) => "Green sat " + s
case c: Color => "Some Color: " + c
case w: Any => "Whatever: " + w
case _ => "Default, but Any captures all"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment