Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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