From 579e999fbf59f540e2d6287fafa7f149ea2b0989 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 23 Nov 2011 13:00:35 +0000 Subject: Preparations for new version of AbstractPartial... Preparations for new version of AbstractPartialFunctions that also does isDefinedAt correctly. Should be a new starr. Review by extempore. --- src/library/scala/PartialFunction.scala | 4 ++++ src/library/scala/runtime/AbstractPartialFunction.scala | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala index ac721b88bc..254a610648 100644 --- a/src/library/scala/PartialFunction.scala +++ b/src/library/scala/PartialFunction.scala @@ -48,6 +48,9 @@ trait PartialFunction[-A, +B] extends (A => B) { else that.apply(x) } + def orElseFast[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] = + orElse(that) + /** Composes this partial function with a transformation function that * gets applied to results of this partial function. * @param k the transformation function @@ -90,6 +93,7 @@ object PartialFunction { 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 override def lift = (x: Any) => None } def empty[A, B] : PartialFunction[A, B] = empty_pf diff --git a/src/library/scala/runtime/AbstractPartialFunction.scala b/src/library/scala/runtime/AbstractPartialFunction.scala index 2a45ec3671..b188cbb37d 100644 --- a/src/library/scala/runtime/AbstractPartialFunction.scala +++ b/src/library/scala/runtime/AbstractPartialFunction.scala @@ -32,11 +32,15 @@ abstract class AbstractPartialFunction[-T1, +R] // 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? - def orElseFast[A1 <: T1, B1 >: R](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] = { + override def orElseFast[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 orElse that + result.fallBack = this.fallBack orElseFast that result } } +/* + def isDefinedAt(x: T1): scala.Boolean = isDefinedAtCurrent(x) || fallBack.isDefinedAt(x) + def isDefinedAtCurrent(x: T1): scala.Boolean = false +*/ } -- cgit v1.2.3