在为 Actor 编写 Specs2 规范时,我MatchError
对几个部分函数的组合感到有些困惑。
一个最小的例子:
val testPf1 = PartialFunction[Any, Boolean]{ case 2 ⇒ true }
val testPf2 = PartialFunction[Any, Boolean]{ case 1 ⇒ true }
val testPf = testPf1 orElse testPf2
testPf.isDefinedAt(1)
testPf.isDefinedAt(2)
testPf(1)
testPf(2)
导致输出:
testPf1: PartialFunction[Any,Boolean] = <function1>
testPf2: PartialFunction[Any,Boolean] = <function1>
testPf: PartialFunction[Any,Boolean] = <function1>
res0: Boolean = true
res1: Boolean = true
scala.MatchError: 1 (of class java.lang.Integer)
at com.dasgip.controller.common.informationmodel.programming.parametersequence.A$A161$A$A161$$anonfun$testPf1$1.apply(PFTest.sc0.tmp:33)
at com.dasgip.controller.common.informationmodel.programming.parametersequence.A$A161$A$A161$$anonfun$testPf1$1.apply(PFTest.sc0.tmp:33)
at scala.PartialFunction$$anonfun$apply$1.applyOrElse(PFTest.sc0.tmp:243)
at scala.PartialFunction$OrElse.apply(PFTest.sc0.tmp:163)
at #worksheet#.#worksheet#(PFTest.sc0.tmp:36)
这完全让我困惑。如果对于一个给定的输入isDefinedAt
组成的两个偏函数返回true
,我希望我也可以将apply
它返回到相同的输入。