diff options
author | Paul Phillips <paulp@improving.org> | 2011-10-19 21:31:55 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-10-19 21:31:55 +0000 |
commit | 5fb68614da51c601e354d13ae123820b355594d0 (patch) | |
tree | 62366806e8678628d1cedede056cf8c3719fb62a /src/library | |
parent | 8337964e312849e5a904b3cfbfa1def0cf180a05 (diff) | |
download | scala-5fb68614da51c601e354d13ae123820b355594d0.tar.gz scala-5fb68614da51c601e354d13ae123820b355594d0.tar.bz2 scala-5fb68614da51c601e354d13ae123820b355594d0.zip |
AbstractPartialFunction.
Contributed by Todd Vierling with minor mods by extempore. This is an
obvious extension of AbstractFunctionN which I had some issue making
work at the time. Sounds kind of pitiful given that the compiler patch
is about two lines, but let's all agree to believe it was a different
world then.
This example program is impacted as follows:
class A {
def f: PartialFunction[Any, Int] = { case x: String => 1 }
def g: PartialFunction[Any, Int] = f orElse { case x: List[_] => 2 }
def h: PartialFunction[Any, Int] = g orElse { case x: Set[_] => 3 }
}
Before: 34943 bytes of bytecode
After: 4217 bytes of bytecode
A mere 88% reduction in size. "'Tis but a trifle!" Closes SI-5096,
SI-5097.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Function.scala | 2 | ||||
-rw-r--r-- | src/library/scala/PartialFunction.scala | 6 | ||||
-rw-r--r-- | src/library/scala/runtime/AbstractPartialFunction.scala | 11 | ||||
-rw-r--r-- | src/library/scala/util/control/Exception.scala | 4 |
4 files changed, 17 insertions, 6 deletions
diff --git a/src/library/scala/Function.scala b/src/library/scala/Function.scala index 23224b27e8..f1e93d6c76 100644 --- a/src/library/scala/Function.scala +++ b/src/library/scala/Function.scala @@ -39,7 +39,7 @@ object Function { * f returns `Some(_)` and undefined where `f` returns `None`. * @see [[scala.PartialFunction#lift]] */ - def unlift[T, R](f: T => Option[R]): PartialFunction[T, R] = new PartialFunction[T, R] { + def unlift[T, R](f: T => Option[R]): PartialFunction[T, R] = new runtime.AbstractPartialFunction[T, R] { def apply(x: T): R = f(x).get def isDefinedAt(x: T): Boolean = f(x).isDefined override def lift: T => Option[R] = f diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala index 51bb3dc93e..69e4dab675 100644 --- a/src/library/scala/PartialFunction.scala +++ b/src/library/scala/PartialFunction.scala @@ -36,7 +36,7 @@ trait PartialFunction[-A, +B] extends (A => B) { * takes `x` to `this(x)` where `this` is defined, and to `that(x)` where it is not. */ def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] = - new PartialFunction[A1, B1] { + new runtime.AbstractPartialFunction[A1, B1] { def isDefinedAt(x: A1): Boolean = PartialFunction.this.isDefinedAt(x) || that.isDefinedAt(x) def apply(x: A1): B1 = @@ -51,7 +51,7 @@ trait PartialFunction[-A, +B] extends (A => B) { * @return a partial function with the same domain as this partial function, which maps * arguments `x` to `k(this(x))`. */ - override def andThen[C](k: B => C) : PartialFunction[A, C] = new PartialFunction[A, C] { + override def andThen[C](k: B => C) : PartialFunction[A, C] = new runtime.AbstractPartialFunction[A, C] { def isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x) def apply(x: A): C = k(PartialFunction.this.apply(x)) } @@ -82,7 +82,7 @@ trait PartialFunction[-A, +B] extends (A => B) { * @since 2.8 */ object PartialFunction { - private[this] final val empty_pf = new PartialFunction[Any, Nothing] { + private[this] final val empty_pf: PartialFunction[Any, Nothing] = new runtime.AbstractPartialFunction[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 diff --git a/src/library/scala/runtime/AbstractPartialFunction.scala b/src/library/scala/runtime/AbstractPartialFunction.scala new file mode 100644 index 0000000000..2f8b54356f --- /dev/null +++ b/src/library/scala/runtime/AbstractPartialFunction.scala @@ -0,0 +1,11 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2011, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.runtime + +abstract class AbstractPartialFunction[-T1, +R] extends AbstractFunction1[T1, R] with PartialFunction[T1, R] diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala index c8470d2504..5fc938d18c 100644 --- a/src/library/scala/util/control/Exception.scala +++ b/src/library/scala/util/control/Exception.scala @@ -229,8 +229,8 @@ object Exception { private def wouldMatch(x: Throwable, classes: collection.Seq[Class[_]]): Boolean = classes exists (_ isAssignableFrom x.getClass) - private def pfFromExceptions(exceptions: Class[_]*) = - new PartialFunction[Throwable, Nothing] { + private def pfFromExceptions(exceptions: Class[_]*): PartialFunction[Throwable, Nothing] = + new scala.runtime.AbstractPartialFunction[Throwable, Nothing] { def apply(x: Throwable) = throw x def isDefinedAt(x: Throwable) = wouldMatch(x, exceptions) } |