Skip to content

Instantly share code, notes, and snippets.

@kevinpet
Last active August 29, 2015 14:04
Show Gist options
  • Save kevinpet/e2f34a67a7a82abaa2c8 to your computer and use it in GitHub Desktop.
Save kevinpet/e2f34a67a7a82abaa2c8 to your computer and use it in GitHub Desktop.
Scala Pattern Matching
object patterns {
abstract class Foo
class Bar(val bar: Int) extends Foo
class Baz(val baz: String) extends Foo
def handle(f: Foo) =
f match {
case b: Bar => b.bar
case b: Baz => b.baz
}
handle(new Bar(42)) //> res0: Any = 42
handle(new Baz("Luhrmann")) //> res1: Any = Luhrmann
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment