While writing a Specs2 specification for an Actor I got a somewhat puzzling MatchError
for composition of several partial functions.
A minimal example:
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)
leads to the output:
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)
That totally confused me. If for a given input isDefinedAt
on the composition of two partial functions returns true
, I would expect that I can also apply
it to the same input.