Skip to content

Instantly share code, notes, and snippets.

@stefanobaghino
Created July 17, 2017 07:21
Show Gist options
  • Save stefanobaghino/fb7c1a4071cc741fcb84af4ed2dc060c to your computer and use it in GitHub Desktop.
Save stefanobaghino/fb7c1a4071cc741fcb84af4ed2dc060c to your computer and use it in GitHub Desktop.
object Person {
def unapply(arg: Person): Option[(String, Int, Boolean)] =
Some(arg.name, arg.age, arg.valid)
}
trait Person {
def name: String
def age: Int
def valid: Boolean
}
case class MarriedPerson(
name: String,
age: Int,
valid: Boolean,
spouse: Person) extends Person
val bob = MarriedPerson("bob", 27, false, null)
val alice = MarriedPerson("alice", 27, true, bob)
println(alice match {
case MarriedPerson("charlotte", _, _, Person("bob", _, _)) => "charlotte is married to bob"
case MarriedPerson("alice", _, _, Person("desmond", _, _)) => "alice is married to desmond"
case MarriedPerson("alice", _, _, Person("bob", _, _)) => "alice is married to bob"
case _ => "no one is married"
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment