3

我试图了解 scala unapply 方法。

以下是我的理解。假设我有一个 Person 对象:

class Person(val fname: String, val lname: String)

object Person{
  def unapply(x: Person) : Option[(String, String)] = 
    Some(x.fname,x.lname)
}

new Person("Magic", "Mike") match {
  case Person(x, y) => s"Last Name is ${y}"
  case _ => "Unknown"
}

我认为这个案例的调用类似于:

val temp = Person.unapply(new Person("Magic", "Mike"))
if (temp != None)  { val (x, y) = temp.get }
else { <go to next case> }

但是当我有以下情况时,下面的 unapply 是如何工作的:

new Person("Magic", "Mike") match {
  case Person("Harold", y) => s"Last Name is ${y}"
  case Person("Magic", y) => s"Last Name is ${y}"
  case _ => "Unknown"
}

它如何在 unapply 方法中访问 fname("Magic") 的值并给我与第一个相同/正确的结果?

4

1 回答 1

4

运行scalacwith-Xprint:patmat将向您展示句法树在模式匹配阶段后的样子:

scalac -Xprint:patmat test.scala

  case <synthetic> val x1: Person = new Person("Magic", "Mike");
  case10(){
    <synthetic> val o12: Option[(String, String)] = Person.unapply(x1);
    if (o12.isEmpty.unary_!)
      {
        <synthetic> val p3: String = o12.get._1;
        val y: String = o12.get._2;
        if ("Harold".==(p3))
          matchEnd9(scala.StringContext.apply("Last Name is ", "").s(y))
        else
          case11()
      }
    else
      case11()
  };
  case11(){
    <synthetic> val o14: Option[(String, String)] = Person.unapply(x1);
    if (o14.isEmpty.unary_!)
      {
        <synthetic> val p5: String = o14.get._1;
        val y: String = o14.get._2;
        if ("Magic".==(p5))
          matchEnd9(scala.StringContext.apply("Last Name is ", "").s(y))
        else
          case13()
      }
    else
      case13()
  };
  case13(){
    matchEnd9("Unknown")
  };

正如你所看到的,对于每种情况,它首先调用unapply匹配的对象,然后如果Option不为空(所以它已经匹配),它检查元组的元素之一是否等于预期值,如果是,那么它匹配到本案结案。

于 2016-12-26T18:31:09.040 回答