From 66b47e1a8c11196d648ed5a98f934a1c65203a65 Mon Sep 17 00:00:00 2001 From: Pavel Pavlov Date: Mon, 16 Jan 2012 07:57:09 +0700 Subject: a fast, functional PartialFunction implementation runtime.AbstractPartialFunction provides a default implementation for the new-style partial function. In principle this class is only subclassed by compiler-generated partial functions arising from matches. Either - the apply method (old-style partialfun) or - the applyOrElse method (current scheme) must be overridden, and the isDefinedAt method implemented. The applyOrElse method implementation is provided to ease the transition from the old scheme, since starr still generates old-style PartialFunctions, but locker's library has the new AbstractPartialFunction. Thus, this implementation is intended as a drop-in replacement for the old partial function, and does not require changes to the compiler. (compiler patches, both for old and new-style pattern matching, follow) - runtime.AbstractPartialFunction is based on PartialFunction.WithDefault Original version of FunctionWithDefault by Odersky (http://article.gmane.org/gmane.comp.lang.scala.internals/4032) - better performance for OrElse#applyOrElse, OrElse#lift, PF.cond - new combinator methods: PF#run, PF#runWith, PF.apply authored by @pavelpavlov, refactored by @adriaanm, review by @paulp --- src/actors/scala/actors/Actor.scala | 8 +++++--- src/actors/scala/actors/Future.scala | 4 ++-- src/actors/scala/actors/Reactor.scala | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/actors') diff --git a/src/actors/scala/actors/Actor.scala b/src/actors/scala/actors/Actor.scala index aab533ae8d..bc9bbc6ef0 100644 --- a/src/actors/scala/actors/Actor.scala +++ b/src/actors/scala/actors/Actor.scala @@ -1,3 +1,5 @@ + + /* __ *\ ** ________ ___ / / ___ Scala API ** ** / __/ __// _ | / / / _ | (c) 2005-2011, LAMP/EPFL ** @@ -246,8 +248,8 @@ object Actor extends Combinators { rawSelf.react(new RecursiveProxyHandler(rawSelf, f)) private class RecursiveProxyHandler(a: InternalReplyReactor, f: PartialFunction[Any, Unit]) - extends scala.runtime.AbstractPartialFunction[Any, Unit] { - def _isDefinedAt(m: Any): Boolean = + extends PartialFunction[Any, Unit] { + def isDefinedAt(m: Any): Boolean = true // events are immediately removed from the mailbox def apply(m: Any) { if (f.isDefinedAt(m)) f(m) @@ -404,5 +406,5 @@ trait Actor extends InternalActor with ReplyReactor { this } -} + } diff --git a/src/actors/scala/actors/Future.scala b/src/actors/scala/actors/Future.scala index eec43013d3..735c13190b 100644 --- a/src/actors/scala/actors/Future.scala +++ b/src/actors/scala/actors/Future.scala @@ -200,8 +200,8 @@ object Futures { Actor.timer.schedule(timerTask, timeout) def awaitWith(partFuns: Seq[PartialFunction[Any, Pair[Int, Any]]]) { - val reaction: PartialFunction[Any, Unit] = new scala.runtime.AbstractPartialFunction[Any, Unit] { - def _isDefinedAt(msg: Any) = msg match { + val reaction: PartialFunction[Any, Unit] = new PartialFunction[Any, Unit] { + def isDefinedAt(msg: Any) = msg match { case TIMEOUT => true case _ => partFuns exists (_ isDefinedAt msg) } diff --git a/src/actors/scala/actors/Reactor.scala b/src/actors/scala/actors/Reactor.scala index 8fc7578344..206a97d97c 100644 --- a/src/actors/scala/actors/Reactor.scala +++ b/src/actors/scala/actors/Reactor.scala @@ -38,8 +38,8 @@ private[actors] object Reactor { } } - val waitingForNone: PartialFunction[Any, Unit] = new scala.runtime.AbstractPartialFunction[Any, Unit] { - def _isDefinedAt(x: Any) = false + val waitingForNone: PartialFunction[Any, Unit] = new PartialFunction[Any, Unit] { + def isDefinedAt(x: Any) = false def apply(x: Any) {} } } -- cgit v1.2.3