Scala 取消应用方法
Scala unapply method
我正在尝试了解 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> }
但是当我有如下情况时,下面的取消应用是如何工作的:
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") 的值并将 same/correct 结果作为第一个给我?
运行 scalac
和 -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
不为空(因此匹配),它检查是否有一个元素元组的值等于预期值,如果是,则它与这种情况的闭包匹配。
我正在尝试了解 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> }
但是当我有如下情况时,下面的取消应用是如何工作的:
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") 的值并将 same/correct 结果作为第一个给我?
运行 scalac
和 -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
不为空(因此匹配),它检查是否有一个元素元组的值等于预期值,如果是,则它与这种情况的闭包匹配。