How does orElse work on PartialFunctions

jedesah picture jedesah · Aug 18, 2014 · Viewed 9.4k times · Source

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

Answer

Gabriele Petronella picture Gabriele Petronella · Aug 19, 2014

There's a few things wrong with your attempt, but first let's see a working implementation:

scala> val a: PartialFunction[String, Unit] = { case "hello" => println("bye") }
a: PartialFunction[String,Unit] = <function1>

scala> val b: PartialFunction[Any, Unit] = { case _ => println("fallback") }
b: PartialFunction[Any,Unit] = <function1>

scala> val c = a.orElse(b)
c: PartialFunction[String,Unit] = <function1>

scala> c("hello")
bye

scala> c("foo")
fallback

There's two main errors in your code:

  1. the way the PF is defined
  2. the (wrong) assumption that empty is a "catch-all" function that returns Nothing

1. How to define a PartialFunction

val right: PartialFunction[String, Unit] = {
  case "hello" => println("bye")
}

How not to define it:

val wrong = PartialFunction[String, Unit] {
  case "hello" => println("bye")
}

If you look at the definition of PartialFunction.apply

def apply[A, B](f: A => B): PartialFunction[A, B] = { case x => f(x) }

you'll see that it defines a partial function for any x and it applies the given f function to it. Now your { case "hello" => println("bye") } is the f argument, so you approximately end up with the following (clearly unexpected) PartialFunction:

val wrong: PartialFunction[String, Unit] = {
  case x => x match {
    case "hello" => println("bye")  
  }

So when you ask whether it's defined it will always return true, since it's defined for any string:

wrong.isDefinedAt("hello") // true (ok)
wrong.isDefinedAt("whatever") // true (sure?)

but when you try to apply it

wrong("hello") // bye (ok)
wrong("whatever") // MatchError (BOOM!)

you fall short on the inner match.

Since orElse decides whether to call the "else" depending on the result of isDefined, then it's obvious why it fails.

2. Empty catches nothing!

Straight from the docs:

def empty[A, B]: PartialFunction[A, B]

The partial function with empty domain. Any attempt to invoke empty partial function leads to throwing scala.MatchError exception.

The PartialFunction (well, it's not really partial anymore) you're looking for is:

val fallback: PartialFunction[Any, Unit] = { case _ => println("fallback") }

or - just to show that we learn from our mistakes -

val fallback = PartialFunction[Any, Unit] { _ => println("fallback") }