{ def unapply(p: Piyo) = Some(p.piyo) } val foo = new Piyo("piyopiyo", "hogehoge") val res3 = foo match { case PiyoPiyo(p) => p case _ => "not piyo..." } res3: String = piyopiyo unapplyが定義されていれば、パターンマッチができる (extractor:抽出子) ※ case class は unapply が自動的に定義される
object US extends Nationality case object China extends Nationality case class Person( name: String, age: Int, admin: Boolean, nationality: Nationality ) val tango = Person("tango", 34, true, Japan) val sato = Person("sato", 23, false, Japan) val john = Person("john", 16, true, US) val wang = Person("wang", 19, false, China)
= p match { case Person(_, _, true, _) 㱺 Some(p) case _ 㱺 None } } // ຊਓʹϚον object Japanese { def unapply(p: Person) = p match { case Person(_, _, _, Japan) 㱺 Some(p) case _ 㱺 None } }