Created
April 3, 2012 07:41
-
-
Save oxlade39/2290211 to your computer and use it in GitHub Desktop.
Example showing unexpected behaviour from a PartialFunction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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