我们可以看到PartialFunction是一个trait,它继承自函数 (A => B), 这个函数有一个参数和一个返回值,在Scala中,该函数会被自动解析为Function1。
我们看下Function1的定义:
traitFunction1[@specialized(scala.Int, scala.Long, scala.Float, scala.Double) -T1, @specialized(scala.Unit, scala.Boolean, scala.Int, scala.Float, scala.Long, scala.Double) +R] extendsAnyRef{ self =>/** Apply the body of this function to the argument. * @return the result of function application.*/defapply(v1: T1):R
val inc = new PartialFunction[Any, Int] {
override def isDefinedAt(x: Any): Boolean = ???
override def apply(v1: Any): Int = ???
}
println("Step 1: Review of Pattern Matching in Scala")
val donut = "Glazed Donut"
val tasteLevel = donut match {
case "Glazed Donut" | "Strawberry Donut" => "Very tasty"
case "Plain Donut" => "Tasty"
case _ => "Tasty"
}
println(s"Taste level of $donut = $tasteLevel")