From 6f881202be35c7d08ca73054594618cef19d8938 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 28 Aug 2011 00:13:17 +0000 Subject: Gave partial function an empty member. --- src/library/scala/PartialFunction.scala | 11 +++++++++-- test/files/run/emptypf.check | 3 +++ test/files/run/emptypf.scala | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/files/run/emptypf.check create mode 100644 test/files/run/emptypf.scala diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala index b8e20c2de1..51bb3dc93e 100644 --- a/src/library/scala/PartialFunction.scala +++ b/src/library/scala/PartialFunction.scala @@ -81,8 +81,15 @@ trait PartialFunction[-A, +B] extends (A => B) { * @author Paul Phillips * @since 2.8 */ -object PartialFunction -{ +object PartialFunction { + private[this] final val empty_pf = new PartialFunction[Any, Nothing] { + def isDefinedAt(x: Any) = false + def apply(x: Any): Nothing = sys.error("undefined") + override def orElse[A1, B1](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] = that + override def lift = (x: Any) => None + } + def empty[A, B] : PartialFunction[A, B] = empty_pf.asInstanceOf[PartialFunction[A, B]] + /** Creates a Boolean test based on a value and a partial function. * It behaves like a 'match' statement with an implied 'case _ => false' * following the supplied cases. diff --git a/test/files/run/emptypf.check b/test/files/run/emptypf.check new file mode 100644 index 0000000000..f6c39921bc --- /dev/null +++ b/test/files/run/emptypf.check @@ -0,0 +1,3 @@ +100 +3 +false diff --git a/test/files/run/emptypf.scala b/test/files/run/emptypf.scala new file mode 100644 index 0000000000..eb3e3e6380 --- /dev/null +++ b/test/files/run/emptypf.scala @@ -0,0 +1,14 @@ +object Test { + val f: PartialFunction[String, Int] = { + PartialFunction.empty[String, Int] orElse { + case "abc" => 100 + case s => s.length + } + } + + def main(args: Array[String]): Unit = { + println(f("abc")) + println(f("def")) + println(PartialFunction.empty[String, Int] isDefinedAt "abc") + } +} -- cgit v1.2.3