I'm trying to replicate the powerful pattern matching example that Joshua Suereth presented in his Devoxx 2013 talk titled "How to wield Scala in the trenches". Unfortunately I cannot achieve what he described and I cannot understand what is wrong. Can someone give me a hint at what I'm missing? (My Scala version is 2.10.3)
Please see the self contained code below:
case class Person(name: String, residence: Seq[Residence]) case class Residence(city: String, country: String) object LivesIn { def unapply(p: Person): Option[Seq[String]] = Some( for(r <- p.residence) yield r.city ) } class StringSeqContains(value: String) { def unapply(in: Seq[String]): Boolean = in contains value } object PatternPower extends App { val people = Seq(Person("Emre", Seq(Residence("Antwerp", "BE"))), Person("Ergin", Seq(Residence("Istanbul", "TR")))) val Istanbul = new StringSeqContains("Istanbul") // #1 does not work as expected, WHY? println( people collect { case person @ LivesIn(Istanbul) => person } ) // #2 works as expected println( people collect { case person @ LivesIn(cities) if cities.contains("Istanbul") => person } ) // #3 works as expected println( people collect { case person @ Person(_, res) if res.contains(Residence("Istanbul", "TR")) => person } ) } When I compile and run it I get:
List() List(Person(Ergin,List(Residence(Istanbul,TR)))) List(Person(Ergin,List(Residence(Istanbul,TR)))) As denoted in the source code, I fail to grasp why the first pattern does not produce the same result as the remaining two pattern matches. Any ideas why?