I am getting very bizarre behavior (at least it seems to me) with the orElse
method defined on PartialFunction
It would seem to me that:
val a = PartialFunction[String, Unit] {
case "hello" => println("Bye")
}
val b: PartialFunction[Any, Unit] = a.orElse(PartialFunction.empty[Any, Unit])
a("hello") // "Bye"
a("bogus") // MatchError
b("bogus") // Nothing
b(true) // Nothing
makes sense but this is not how it is behaving and I am having a lot of trouble understanding why as the types signatures seem to indicate what I exposed above.
Here is a transcript of what I am observing with Scala 2.11.2:
Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_11).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a = PartialFunction[String, Unit] {
| case "hello" => println("Bye")
| }
a: PartialFunction[String,Unit] = <function1>
scala> a("hello")
Bye
scala> a("bye")
scala.MatchError: bye (of class java.lang.String)
at $anonfun$1.apply(<console>:7)
at $anonfun$1.apply(<console>:7)
at scala.PartialFunction$$anonfun$apply$1.applyOrElse(PartialFunction.scala:242)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
... 33 elided
scala> val b = a.orElse(PartialFunction.empty[Any, Unit])
b: PartialFunction[String,Unit] = <function1>
scala> b("sdf")
scala.MatchError: sdf (of class java.lang.String)
at $anonfun$1.apply(<console>:7)
at $anonfun$1.apply(<console>:7)
at scala.PartialFunction$$anonfun$apply$1.applyOrElse(PartialFunction.scala:242)
at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
... 33 elided
Note the return type of val b
which has not widen the type of the PartialFunction.
But this also does not work as expected:
scala> val c = a.orElse(PartialFunction.empty[String, Unit])
c: PartialFunction[String,Unit] = <function1>
scala> c("sdfsdf")
scala.MatchError: sdfsdf (of class java.lang.String)
at $anonfun$1.apply(<console>:7)
at $anonfun$1.apply(<console>:7)
at scala.PartialFunction$$anonfun$apply$1.applyOrElse(PartialFunction.scala:242)
at scala.PartialFunction$OrElse.apply(PartialFunction.scala:162)
... 33 elided
PartialFunction.empty[A,B]
is equivalent to:(This typechecks, because
Nothing
is a subtype of bothA
andB
.)or, equivalently:
This cannot match anything.
.orElse
can be understood to simply concatenates lists of case statements from twoPartialFunction
s. So, in your casea.orElse(PartialFunction.empty[Any,Unit]
means:which simplifies to:
or
MatchError
is therefore obvious.Note that the documetation also mentions that
empty
always throwsMatchError
.From what I can guess, you wanted a
PartialFunction
that always matches. There's no named method in the standard library for that, but why should there be. You can simply writeThere's a few things wrong with your attempt, but first let's see a working implementation:
There's two main errors in your code:
empty
is a "catch-all" function that returnsNothing
1. How to define a PartialFunction
How not to define it:
If you look at the definition of
PartialFunction.apply
you'll see that it defines a partial function for any
x
and it applies the givenf
function to it. Now your{ case "hello" => println("bye") }
is thef
argument, so you approximately end up with the following (clearly unexpected)PartialFunction
:So when you ask whether it's defined it will always return true, since it's defined for any string:
but when you try to
apply
ityou fall short on the inner match.
Since
orElse
decides whether to call the "else" depending on the result ofisDefined
, then it's obvious why it fails.2. Empty catches nothing!
Straight from the docs:
The
PartialFunction
(well, it's not really partial anymore) you're looking for is:or - just to show that we learn from our mistakes -
You are using the
PartialFunction
object apply method which is defined so:Basically it takes a function form
A
toB
and automatically wrap it in a case statement, the problem is that you are passing the case too and I'm not 100% sure of what happens then, you can try passing a function to the apply or easily you can try without using the apply method:You could also extend the trait and implement
apply
andisDefined
as shown here.