Last active
August 29, 2015 14:04
-
-
Save kevinpet/d29a39219ba22b5b0f80 to your computer and use it in GitHub Desktop.
Scala case class field extraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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