summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2011-11-24 09:25:00 +0000
committerMartin Odersky <odersky@gmail.com>2011-11-24 09:25:00 +0000
commit5fb26c6a889cf1609823338df8783bf880769b3f (patch)
tree652d5c036465b669dc09039392ce79ab8f4b7759 /src/library
parent32a753546e0f7ef30e3e9c08b39a503ea93bc95a (diff)
downloadscala-5fb26c6a889cf1609823338df8783bf880769b3f.tar.gz
scala-5fb26c6a889cf1609823338df8783bf880769b3f.tar.bz2
scala-5fb26c6a889cf1609823338df8783bf880769b3f.zip
Fast PartialFunction # orElse.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/Function.scala2
-rw-r--r--src/library/scala/PartialFunction.scala7
-rw-r--r--src/library/scala/runtime/AbstractPartialFunction.scala24
-rw-r--r--src/library/scala/util/control/Exception.scala2
4 files changed, 21 insertions, 14 deletions
diff --git a/src/library/scala/Function.scala b/src/library/scala/Function.scala
index f1e93d6c76..4a10b65735 100644
--- a/src/library/scala/Function.scala
+++ b/src/library/scala/Function.scala
@@ -41,7 +41,7 @@ object Function {
*/
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
+ 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 254a610648..b2910c2278 100644
--- a/src/library/scala/PartialFunction.scala
+++ b/src/library/scala/PartialFunction.scala
@@ -41,7 +41,7 @@ trait PartialFunction[-A, +B] extends (A => B) {
*/
def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] =
new runtime.AbstractPartialFunction[A1, B1] {
- def isDefinedAt(x: A1): Boolean =
+ def _isDefinedAt(x: A1): Boolean =
PartialFunction.this.isDefinedAt(x) || that.isDefinedAt(x)
def apply(x: A1): B1 =
if (PartialFunction.this.isDefinedAt(x)) PartialFunction.this.apply(x)
@@ -59,7 +59,7 @@ trait PartialFunction[-A, +B] extends (A => B) {
* arguments `x` to `k(this(x))`.
*/
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 _isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x)
def apply(x: A): C = k(PartialFunction.this.apply(x))
}
@@ -90,7 +90,8 @@ trait PartialFunction[-A, +B] extends (A => B) {
*/
object PartialFunction {
private[this] final val empty_pf: PartialFunction[Any, Nothing] = new runtime.AbstractPartialFunction[Any, Nothing] {
- def isDefinedAt(x: Any) = false
+ def _isDefinedAt(x: Any) = false
+ override def isDefinedAt(x: Any) = false
def apply(x: Any): Nothing = throw new MatchError(x)
override def orElse[A1, B1](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] = that
override def orElseFast[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
index b188cbb37d..f48d99f5af 100644
--- a/src/library/scala/runtime/AbstractPartialFunction.scala
+++ b/src/library/scala/runtime/AbstractPartialFunction.scala
@@ -23,24 +23,30 @@ abstract class AbstractPartialFunction[-T1, +R]
with PartialFunction[T1, R]
with Cloneable {
- private var fallBack: PartialFunction[T1 @uncheckedVariance, R @uncheckedVariance] = PartialFunction.empty
+ private var fallBackField: PartialFunction[T1 @uncheckedVariance, R @uncheckedVariance] = _
- override protected def missingCase(x: T1): R = synchronized {
- fallBack(x)
+ def fallBack: PartialFunction[T1, R] = synchronized {
+ if (fallBackField == null) fallBackField = PartialFunction.empty
+ fallBackField
}
+ override protected def missingCase(x: T1): R = fallBack(x)
+
// Question: Need to ensure that fallBack is overwritten before any access
// Is the `synchronized` here the right thing to achieve this?
// Is there a cheaper way?
- override def orElseFast[A1 <: T1, B1 >: R](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] = {
+ override def orElse[A1 <: T1, B1 >: R](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] = {
val result = this.clone.asInstanceOf[AbstractPartialFunction[A1, B1]]
result.synchronized {
- result.fallBack = this.fallBack orElseFast that
+ result.fallBackField = this.fallBackField orElse that
result
}
}
-/*
- def isDefinedAt(x: T1): scala.Boolean = isDefinedAtCurrent(x) || fallBack.isDefinedAt(x)
- def isDefinedAtCurrent(x: T1): scala.Boolean = false
-*/
+
+ def isDefinedAt(x: T1): scala.Boolean = _isDefinedAt(x) || fallBack.isDefinedAt(x)
+ def _isDefinedAt(x: T1): scala.Boolean
+
}
+
+
+
diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala
index 5fc938d18c..5e3f8b6451 100644
--- a/src/library/scala/util/control/Exception.scala
+++ b/src/library/scala/util/control/Exception.scala
@@ -232,6 +232,6 @@ object Exception {
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)
+ def _isDefinedAt(x: Throwable) = wouldMatch(x, exceptions)
}
}