Skip to content

Instantly share code, notes, and snippets.

@oxlade39
Created April 3, 2012 07:41
Show Gist options
  • Save oxlade39/2290211 to your computer and use it in GitHub Desktop.
Save oxlade39/2290211 to your computer and use it in GitHub Desktop.
Example showing unexpected behaviour from a PartialFunction
class ScalaSpec extends FlatSpec with MustMatchers {
behavior of "a PartialFunction"
it should "chain" in {
var pf: PartialFunction[String, String] = {
case "1" => "2"
}
pf = pf andThen {
case "2" => "3"
}
pf("1") must be ("3")
var pf2: PartialFunction[String, Option[String]] = {
case "1" => Some("2")
}
val pf3 = pf2 andThen {
case None => "3"
}
pf3.isDefinedAt("1") must be(false) // this is true
pf3("1") // which means this fails with a MatchError
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment