Skip to content

Instantly share code, notes, and snippets.

@kevinpet
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinpet/d29a39219ba22b5b0f80 to your computer and use it in GitHub Desktop.
Save kevinpet/d29a39219ba22b5b0f80 to your computer and use it in GitHub Desktop.
Scala case class field extraction
object patterns {
abstract class Foo
case class Bar(val bar: Int) extends Foo
case class Baz(val baz: String) extends Foo
def handle(f: Foo) =
f match {
case Bar(i) if (i > 10) => 10
case Bar(i) => i
case Baz(s) => s
}
handle(new Bar(42)) //> res0: Any = 10
handle(new Bar(3)) //> res0: Any = 3
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