diff options
author | Antonio Cunei <antonio.cunei@epfl.ch> | 2009-11-09 16:13:33 +0000 |
---|---|---|
committer | Antonio Cunei <antonio.cunei@epfl.ch> | 2009-11-09 16:13:33 +0000 |
commit | e981bccdb7874726af0896efdadb5e192d25c14a (patch) | |
tree | 68ec4c2c9764a935c7efd72cf00a7206b6e18166 | |
parent | df502f4ffa5ebf83e7e97a270504202e00f512ec (diff) | |
download | scala-e981bccdb7874726af0896efdadb5e192d25c14a.tar.gz scala-e981bccdb7874726af0896efdadb5e192d25c14a.tar.bz2 scala-e981bccdb7874726af0896efdadb5e192d25c14a.zip |
Adds isDefinedAt to Function1. As a consequence,
code that mixes in PartialFunction now have to
define isDefinedAt as override.
Fixes #2225.
-rw-r--r-- | src/actors/scala/actors/Actor.scala | 2 | ||||
-rw-r--r-- | src/actors/scala/actors/Future.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/UnCurry.scala | 19 | ||||
-rw-r--r-- | src/library/scala/Function1.scala | 6 | ||||
-rw-r--r-- | src/library/scala/PartialFunction.scala | 11 | ||||
-rw-r--r-- | src/library/scala/collection/MapLike.scala | 2 | ||||
-rw-r--r-- | src/library/scala/collection/SeqLike.scala | 4 | ||||
-rw-r--r-- | src/library/scala/util/control/Exception.scala | 8 | ||||
-rw-r--r-- | src/swing/scala/swing/Reactions.scala | 4 |
9 files changed, 32 insertions, 26 deletions
diff --git a/src/actors/scala/actors/Actor.scala b/src/actors/scala/actors/Actor.scala index b563104f41..69a4aab7d7 100644 --- a/src/actors/scala/actors/Actor.scala +++ b/src/actors/scala/actors/Actor.scala @@ -210,7 +210,7 @@ object Actor { private class RecursiveProxyHandler(a: Reactor, f: PartialFunction[Any, Unit]) extends PartialFunction[Any, Unit] { - def isDefinedAt(m: Any): Boolean = + override def isDefinedAt(m: Any): Boolean = true // events are immediately removed from the mailbox def apply(m: Any) { if (f.isDefinedAt(m)) f(m) diff --git a/src/actors/scala/actors/Future.scala b/src/actors/scala/actors/Future.scala index 38b268d795..63007b6477 100644 --- a/src/actors/scala/actors/Future.scala +++ b/src/actors/scala/actors/Future.scala @@ -105,7 +105,7 @@ object Futures { def awaitWith(partFuns: Seq[PartialFunction[Any, Pair[Int, Any]]]) { val reaction: PartialFunction[Any, Unit] = new PartialFunction[Any, Unit] { - def isDefinedAt(msg: Any) = msg match { + override def isDefinedAt(msg: Any) = msg match { case TIMEOUT => true case _ => partFuns exists (_ isDefinedAt msg) } diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index eec523a2b8..36ca4f399d 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -282,7 +282,7 @@ abstract class UnCurry extends InfoTransform with TypingTransformers { * class $anon() extends Object() with PartialFunction[T, R] with ScalaObject { * def apply(x: T): R = (x: @unchecked) match { * { case P_i if G_i => E_i }_i=1..n - * def isDefinedAt(x: T): boolean = (x: @unchecked) match { + * override def isDefinedAt(x: T): boolean = (x: @unchecked) match { * case P_1 if G_1 => true * ... * case P_n if G_n => true @@ -291,9 +291,12 @@ abstract class UnCurry extends InfoTransform with TypingTransformers { * } * new $anon() * - * However, if one of the patterns P_i if G_i is a default pattern, generate instead + * However, if one of the patterns P_i if G_i is a default pattern, it should generate instead * - * def isDefinedAt(x: T): boolean = true + * override def isDefinedAt(x: T): boolean = true + * + * which is the default in Function1 (and PartialFunction) anyway, so + * no overridden def is emitted. */ def transformFunction(fun: Function): Tree = { val fun1 = deEta(fun) @@ -335,7 +338,7 @@ abstract class UnCurry extends InfoTransform with TypingTransformers { } val members = { if (fun.tpe.typeSymbol == PartialFunctionClass) { - val isDefinedAtMethod = anonClass.newMethod(fun.pos, nme.isDefinedAt).setFlag(FINAL) + val isDefinedAtMethod = anonClass.newMethod(fun.pos, nme.isDefinedAt).setFlag(FINAL | OVERRIDE) isDefinedAtMethod.setInfo(MethodType(isDefinedAtMethod.newSyntheticValueParams(formals), BooleanClass.tpe)) anonClass.info.decls enter isDefinedAtMethod @@ -353,8 +356,12 @@ abstract class UnCurry extends InfoTransform with TypingTransformers { (cases map transformCase) ::: List(CaseDef(Ident(nme.WILDCARD), EmptyTree, Literal(false)))) } - List(applyMethodDef(mkUnchecked(fun.body)), - DefDef(isDefinedAtMethod, mkUnchecked(idbody(isDefinedAtMethod.paramss.head.head)))) + val isDef=idbody(isDefinedAtMethod.paramss.head.head) + if (isDef == Literal(true)) + List(applyMethodDef(mkUnchecked(fun.body))) + else + List(applyMethodDef(mkUnchecked(fun.body)), + DefDef(isDefinedAtMethod, mkUnchecked(isDef))) } else { List(applyMethodDef(fun.body)) } diff --git a/src/library/scala/Function1.scala b/src/library/scala/Function1.scala index 7cfd32304e..80fe7ad1b6 100644 --- a/src/library/scala/Function1.scala +++ b/src/library/scala/Function1.scala @@ -47,4 +47,10 @@ trait Function1[-T1, +R] extends AnyRef { self => */ def andThen[A](g: R => A): T1 => A = { x => g(apply(x)) } + /** Checks if a value is contained in the functions domain. + * + * @param x the value to test + * @return true, iff <code>x</code> is in the domain of this function. + */ + def isDefinedAt(x: T1): Boolean = true } diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala index 0ba7527976..1eb5f89fa4 100644 --- a/src/library/scala/PartialFunction.scala +++ b/src/library/scala/PartialFunction.scala @@ -21,16 +21,9 @@ package scala */ trait PartialFunction[-A, +B] extends AnyRef with (A => B) { - /** Checks if a value is contained in the functions domain. - * - * @param x the value to test - * @return true, iff <code>x</code> is in the domain of this function. - */ - def isDefinedAt(x: A): Boolean - def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] = new PartialFunction[A1, B1] { - def isDefinedAt(x: A1): Boolean = + override 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) @@ -38,7 +31,7 @@ trait PartialFunction[-A, +B] extends AnyRef with (A => B) { } override def andThen[C](k: B => C) : PartialFunction[A, C] = new PartialFunction[A, C] { - def isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x) + override def isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x) def apply(x: A): C = k(PartialFunction.this.apply(x)) } } diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala index 3b188acab6..8c44c374ca 100644 --- a/src/library/scala/collection/MapLike.scala +++ b/src/library/scala/collection/MapLike.scala @@ -122,7 +122,7 @@ self => * @param key the key * @return <code>true</code> iff there is a mapping for key in this map */ - def isDefinedAt(key: A) = contains(key) + override def isDefinedAt(key: A) = contains(key) /** @return the keys of this map as a set. */ def keySet: Set[A] = new DefaultKeySet diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala index 708764e958..3413cb8a07 100644 --- a/src/library/scala/collection/SeqLike.scala +++ b/src/library/scala/collection/SeqLike.scala @@ -109,7 +109,7 @@ object SeqLike { * @version 1.0, 16/07/2003 * @since 2.8 */ -trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => +trait SeqLike[+A, +Repr] extends Function[Int, A] with IterableLike[A, Repr] { self => override protected[this] def thisCollection: Seq[A] = this.asInstanceOf[Seq[A]] override protected[this] def toCollection(repr: Repr): Seq[A] = repr.asInstanceOf[Seq[A]] @@ -147,7 +147,7 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => /** Is this partial function defined for the index <code>x</code>? */ - def isDefinedAt(x: Int): Boolean = (x >= 0) && (x < length) + override def isDefinedAt(x: Int): Boolean = (x >= 0) && (x < length) /** Returns length of longest segment starting from a start index `from` * such that every element of the segment satisfies predicate `p`. diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala index 356b11df51..8add16fb70 100644 --- a/src/library/scala/util/control/Exception.scala +++ b/src/library/scala/util/control/Exception.scala @@ -31,7 +31,7 @@ object Exception // a Throwable => T and simply rethrow the non-Exceptions. implicit def fromExceptionCatcher[T](pf: ExceptionCatcher[T]): Catcher[T] = { new PartialFunction[Throwable, T] { - def isDefinedAt(x: Throwable) = x match { + override def isDefinedAt(x: Throwable) = x match { case e: Exception if pf.isDefinedAt(e) => true case _ => false } @@ -102,7 +102,7 @@ object Exception * but with the supplied apply method replacing the current one. */ def withApply[U](f: (Throwable) => U): Catch[U] = { val pf2 = new PartialFunction[Throwable, U] { - def isDefinedAt(x: Throwable) = pf isDefinedAt x + override def isDefinedAt(x: Throwable) = pf isDefinedAt x def apply(x: Throwable) = f(x) } new Catch(pf2, fin) @@ -141,7 +141,7 @@ object Exception final val nothingCatcher: PartialFunction[Throwable, Nothing] = new PartialFunction[Throwable, Nothing] { - def isDefinedAt(x: Throwable) = false + override def isDefinedAt(x: Throwable) = false def apply(x: Throwable) = throw x } @@ -209,6 +209,6 @@ object Exception private def pfFromExceptions(exceptions: Class[_]*) = new PartialFunction[Throwable, Nothing] { def apply(x: Throwable) = throw x - def isDefinedAt(x: Throwable) = wouldMatch(x, exceptions) + override def isDefinedAt(x: Throwable) = wouldMatch(x, exceptions) } } diff --git a/src/swing/scala/swing/Reactions.scala b/src/swing/scala/swing/Reactions.scala index dc7cb2d2f1..fbb20ada9f 100644 --- a/src/swing/scala/swing/Reactions.scala +++ b/src/swing/scala/swing/Reactions.scala @@ -19,7 +19,7 @@ object Reactions { class Impl extends Reactions { private val parts: Buffer[Reaction] = new ListBuffer[Reaction] - def isDefinedAt(e: Event) = parts.exists(_ isDefinedAt e) + override def isDefinedAt(e: Event) = parts.exists(_ isDefinedAt e) def += (r: Reaction): this.type = { parts += r; this } def -= (r: Reaction): this.type = { parts -= r; this } def apply(e: Event) { @@ -36,7 +36,7 @@ object Reactions { class Wrapper(listener: Any)(r: Reaction) extends Reaction with StronglyReferenced with Proxy { def self = listener - def isDefinedAt(e: Event) = r.isDefinedAt(e) + override def isDefinedAt(e: Event) = r.isDefinedAt(e) def apply(e: Event) { r(e) } } } |