Skip to content

Commit 5fb6861

Browse files
committed
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.
1 parent 8337964 commit 5fb6861

File tree

12 files changed

+25
-13
lines changed

12 files changed

+25
-13
lines changed

src/actors/scala/actors/Actor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ object Actor extends Combinators {
246246
rawSelf.react(new RecursiveProxyHandler(rawSelf, f))
247247

248248
private class RecursiveProxyHandler(a: ReplyReactor, f: PartialFunction[Any, Unit])
249-
extends PartialFunction[Any, Unit] {
249+
extends scala.runtime.AbstractPartialFunction[Any, Unit] {
250250
def isDefinedAt(m: Any): Boolean =
251251
true // events are immediately removed from the mailbox
252252
def apply(m: Any) {

src/actors/scala/actors/Future.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ object Futures {
200200
Actor.timer.schedule(timerTask, timeout)
201201

202202
def awaitWith(partFuns: Seq[PartialFunction[Any, Pair[Int, Any]]]) {
203-
val reaction: PartialFunction[Any, Unit] = new PartialFunction[Any, Unit] {
203+
val reaction: PartialFunction[Any, Unit] = new scala.runtime.AbstractPartialFunction[Any, Unit] {
204204
def isDefinedAt(msg: Any) = msg match {
205205
case TIMEOUT => true
206206
case _ => partFuns exists (_ isDefinedAt msg)

src/actors/scala/actors/Reactor.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ private[actors] object Reactor {
3838
}
3939
}
4040

41-
val waitingForNone = new PartialFunction[Any, Unit] {
41+
val waitingForNone: PartialFunction[Any, Unit] = new scala.runtime.AbstractPartialFunction[Any, Unit] {
4242
def isDefinedAt(x: Any) = false
4343
def apply(x: Any) {}
4444
}
45-
4645
}
4746

4847
/**

src/compiler/scala/reflect/internal/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ trait Definitions extends reflect.api.StandardDefinitions {
186186
// fundamental reference classes
187187
lazy val ScalaObjectClass = getMember(ScalaPackageClass, tpnme.ScalaObject)
188188
lazy val PartialFunctionClass = getClass("scala.PartialFunction")
189+
lazy val AbstractPartialFunctionClass = getClass("scala.runtime.AbstractPartialFunction")
189190
lazy val SymbolClass = getClass("scala.Symbol")
190191
lazy val StringClass = getClass(sn.String)
191192
lazy val StringModule = StringClass.linkedClassOfClass

src/compiler/scala/tools/cmd/FromString.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import scala.reflect.OptManifest
1414
* example instances are in the companion object, but in general
1515
* either IntFromString will suffice or you'll want custom transformers.
1616
*/
17-
abstract class FromString[+T](implicit m: OptManifest[T]) extends PartialFunction[String, T] {
17+
abstract class FromString[+T](implicit m: OptManifest[T]) extends scala.runtime.AbstractPartialFunction[String, T] {
1818
def apply(s: String): T
1919
def isDefinedAt(s: String): Boolean = true
2020
def zero: T = apply("")

src/compiler/scala/tools/nsc/interpreter/AbstractOrMissingHandler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package scala.tools.nsc
77
package interpreter
88

9-
class AbstractOrMissingHandler[T](onError: String => Unit, value: T) extends PartialFunction[Throwable, T] {
9+
class AbstractOrMissingHandler[T](onError: String => Unit, value: T) extends scala.runtime.AbstractPartialFunction[Throwable, T] {
1010
def isDefinedAt(t: Throwable) = t match {
1111
case _: AbstractMethodError => true
1212
case _: NoSuchMethodError => true

src/compiler/scala/tools/nsc/matching/ParallelMatching.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ trait ParallelMatching extends ast.TreeDSL
422422
// Should the given pattern join the expanded pivot in the success matrix? If so,
423423
// this partial function will be defined for the pattern, and the result of the apply
424424
// is the expanded sequence of new patterns.
425-
lazy val successMatrixFn = new PartialFunction[Pattern, List[Pattern]] {
425+
lazy val successMatrixFn = new scala.runtime.AbstractPartialFunction[Pattern, List[Pattern]] {
426426
private def seqIsDefinedAt(x: SequenceLikePattern) = (hasStar, x.hasStar) match {
427427
case (true, true) => true
428428
case (true, false) => pivotLen <= x.nonStarLength

src/compiler/scala/tools/nsc/transform/UnCurry.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ abstract class UnCurry extends InfoTransform
247247
val anonClass = owner newAnonymousFunctionClass fun.pos setFlag (FINAL | SYNTHETIC | inConstructorFlag)
248248
def parents =
249249
if (isFunctionType(fun.tpe)) List(abstractFunctionForFunctionType(fun.tpe), SerializableClass.tpe)
250+
else if (isPartial) List(appliedType(AbstractPartialFunctionClass.typeConstructor, targs), SerializableClass.tpe)
250251
else List(ObjectClass.tpe, fun.tpe, SerializableClass.tpe)
251252

252253
anonClass setInfo ClassInfoType(parents, new Scope, anonClass)

src/library/scala/Function.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object Function {
3939
* f returns `Some(_)` and undefined where `f` returns `None`.
4040
* @see [[scala.PartialFunction#lift]]
4141
*/
42-
def unlift[T, R](f: T => Option[R]): PartialFunction[T, R] = new PartialFunction[T, R] {
42+
def unlift[T, R](f: T => Option[R]): PartialFunction[T, R] = new runtime.AbstractPartialFunction[T, R] {
4343
def apply(x: T): R = f(x).get
4444
def isDefinedAt(x: T): Boolean = f(x).isDefined
4545
override def lift: T => Option[R] = f

src/library/scala/PartialFunction.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ trait PartialFunction[-A, +B] extends (A => B) {
3636
* takes `x` to `this(x)` where `this` is defined, and to `that(x)` where it is not.
3737
*/
3838
def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] =
39-
new PartialFunction[A1, B1] {
39+
new runtime.AbstractPartialFunction[A1, B1] {
4040
def isDefinedAt(x: A1): Boolean =
4141
PartialFunction.this.isDefinedAt(x) || that.isDefinedAt(x)
4242
def apply(x: A1): B1 =
@@ -51,7 +51,7 @@ trait PartialFunction[-A, +B] extends (A => B) {
5151
* @return a partial function with the same domain as this partial function, which maps
5252
* arguments `x` to `k(this(x))`.
5353
*/
54-
override def andThen[C](k: B => C) : PartialFunction[A, C] = new PartialFunction[A, C] {
54+
override def andThen[C](k: B => C) : PartialFunction[A, C] = new runtime.AbstractPartialFunction[A, C] {
5555
def isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x)
5656
def apply(x: A): C = k(PartialFunction.this.apply(x))
5757
}
@@ -82,7 +82,7 @@ trait PartialFunction[-A, +B] extends (A => B) {
8282
* @since 2.8
8383
*/
8484
object PartialFunction {
85-
private[this] final val empty_pf = new PartialFunction[Any, Nothing] {
85+
private[this] final val empty_pf: PartialFunction[Any, Nothing] = new runtime.AbstractPartialFunction[Any, Nothing] {
8686
def isDefinedAt(x: Any) = false
8787
def apply(x: Any): Nothing = sys.error("undefined")
8888
override def orElse[A1, B1](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] = that

0 commit comments

Comments
 (0)