From 5355f3c7323fa0ea1dabe2e39ec112c9d0b061b9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 24 Jul 2007 17:43:47 +0000 Subject: fixed problems in subtyping. fixed unsafe isInstanceOf's --- .../scala/collection/immutable/ListSet.scala | 9 ++++---- src/library/scala/collection/immutable/Stack.scala | 6 ++++-- .../scala/collection/mutable/LinkedList.scala | 7 ++++--- .../scala/collection/mutable/PriorityQueue.scala | 12 ++++++----- src/library/scala/collection/mutable/Queue.scala | 12 ++++++----- src/library/scala/collection/mutable/Stack.scala | 12 ++++++----- src/library/scala/reflect/Print.scala | 24 ++++++++++++---------- src/library/scala/xml/Utility.scala | 5 ++++- 8 files changed, 51 insertions(+), 36 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/collection/immutable/ListSet.scala b/src/library/scala/collection/immutable/ListSet.scala index 6bf4d5b626..3945612143 100644 --- a/src/library/scala/collection/immutable/ListSet.scala +++ b/src/library/scala/collection/immutable/ListSet.scala @@ -86,12 +86,13 @@ class ListSet[A] extends AnyRef with Set[A] { /** Compares two sets for equality. * Two set are equal iff they contain the same elements. */ - override def equals(obj: Any): Boolean = - if (obj.isInstanceOf[scala.collection.Set[A]]) { + override def equals(obj: Any): Boolean = obj match { + case _: scala.collection.Set[_] => val that = obj.asInstanceOf[scala.collection.Set[A]] - if (size != that.size) false else toList.forall(that.contains) - } else + (size == that.size) && (toList forall that.contains) + case _ => false + } /** * @throws Predef.NoSuchElementException diff --git a/src/library/scala/collection/immutable/Stack.scala b/src/library/scala/collection/immutable/Stack.scala index 22f257b99f..75d6cb8b98 100644 --- a/src/library/scala/collection/immutable/Stack.scala +++ b/src/library/scala/collection/immutable/Stack.scala @@ -106,8 +106,10 @@ class Stack[+A] extends Seq[A] { * @return true, iff the two stacks are equal; i.e. they contain the * same elements in the same order. */ - override def equals(obj: Any): Boolean = - obj.isInstanceOf[Stack[A]] && sameElements(obj.asInstanceOf[Stack[A]]) + override def equals(obj: Any): Boolean = obj match { + case that: Stack[_] => this sameElements that + case _ => false + } /** Returns the hash code for this stack. * diff --git a/src/library/scala/collection/mutable/LinkedList.scala b/src/library/scala/collection/mutable/LinkedList.scala index bd5ae23532..0ed1a02889 100644 --- a/src/library/scala/collection/mutable/LinkedList.scala +++ b/src/library/scala/collection/mutable/LinkedList.scala @@ -22,9 +22,10 @@ class LinkedList[A](var elem: A, var next: LinkedList[A]) extends SingleLinkedList[A, LinkedList[A]] { - override def equals(obj: Any): Boolean = - obj.isInstanceOf[LinkedList[A]] && - toList.equals((obj.asInstanceOf[LinkedList[A]]).toList) + override def equals(obj: Any): Boolean = obj match { + case that: LinkedList[_] => this.toList equals that.toList + case _ => false + } override protected def stringPrefix: String = "LinkedList" } diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index 0772c545e9..de615469d8 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -153,12 +153,14 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol * * @return true, iff both queues contain the same sequence of elements. */ - override def equals(that: Any): Boolean = - that.isInstanceOf[PriorityQueue[A]] && - { val other = that.asInstanceOf[PriorityQueue[A]] - elements.zip(other.elements).forall { + override def equals(obj: Any): Boolean = obj match { + case that: PriorityQueue[_] => + (this.elements zip that.elements) forall { case (thiselem, thatelem) => thiselem == thatelem - }} + } + case _ => + false + } /** The hashCode method always yields an error, since it is not * safe to use mutable queues as keys in hash tables. diff --git a/src/library/scala/collection/mutable/Queue.scala b/src/library/scala/collection/mutable/Queue.scala index d654a82888..e7c80ae7b4 100644 --- a/src/library/scala/collection/mutable/Queue.scala +++ b/src/library/scala/collection/mutable/Queue.scala @@ -166,12 +166,14 @@ class Queue[A] extends MutableList[A] with CloneableCollection { * * @return true, iff both queues contain the same sequence of elements. */ - override def equals(that: Any): Boolean = - that.isInstanceOf[Queue[A]] && - { val other = that.asInstanceOf[Queue[A]] - elements.zip(other.elements).forall { + override def equals(obj: Any): Boolean = obj match { + case that: Queue[_] => + (this.elements zip that.elements) forall { case (thiselem, thatelem) => thiselem == thatelem - }} + } + case _ => + false + } /** The hashCode method always yields an error, since it is not * safe to use mutable queues as keys in hash tables. diff --git a/src/library/scala/collection/mutable/Stack.scala b/src/library/scala/collection/mutable/Stack.scala index be95aee1a5..8aa2f27115 100644 --- a/src/library/scala/collection/mutable/Stack.scala +++ b/src/library/scala/collection/mutable/Stack.scala @@ -107,12 +107,14 @@ class Stack[A] extends MutableList[A] with CloneableCollection { * * @return true, iff both stacks contain the same sequence of elements. */ - override def equals(that: Any): Boolean = - that.isInstanceOf[Stack[A]] && - { val other = that.asInstanceOf[Stack[A]]; - elements.zip(other.elements).forall { + override def equals(obj: Any): Boolean = obj match { + case that: Stack[_] => + (this.elements zip that.elements) forall { case (thiselem, thatelem) => thiselem == thatelem - }} + } + case _ => + false + } /** The hashCode method always yields an error, since it is not * safe to use mutable stacks as keys in hash tables. diff --git a/src/library/scala/reflect/Print.scala b/src/library/scala/reflect/Print.scala index 789d112490..039160077c 100644 --- a/src/library/scala/reflect/Print.scala +++ b/src/library/scala/reflect/Print.scala @@ -13,18 +13,20 @@ package scala.reflect object Print extends Function1[Any, String] { - def apply(any: Any): String = - if (any.isInstanceOf[Code[Any]]) - apply(any.asInstanceOf[Code[Any]]) - else if (any.isInstanceOf[Tree]) - apply(any.asInstanceOf[Tree]) - else if (any.isInstanceOf[Symbol]) - apply(any.asInstanceOf[Symbol]) - else if (any.isInstanceOf[Type]) - apply(any.asInstanceOf[Type]) - else "UnknownAny" + def apply(any: Any): String = any match { + case x: Code[_] => + apply(x) + case x: Tree => + apply(x) + case x: Symbol => + apply(x) + case x: Type => + apply(x) + case _ => + "UnknownAny" + } - def apply(code: Code[Any]): String = + def apply[A](code: Code[A]): String = Print(code.tree) def apply(tree: Tree): String = tree match { diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala index 00f93b9ffc..3f3ac9d6e8 100644 --- a/src/library/scala/xml/Utility.scala +++ b/src/library/scala/xml/Utility.scala @@ -219,7 +219,10 @@ object Utility extends AnyRef with parsing.TokenTests { sb: StringBuilder, stripComment: Boolean) { if (children.isEmpty) return - else if (children forall { y => y.isInstanceOf[Atom[Any]] && !y.isInstanceOf[Text] }) { // add space + else if (children forall { + case y: Atom[_] => !y.isInstanceOf[Text] + case _ => false + }) { // add space val it = children.elements val f = it.next toXML(f, f.scope, sb, stripComment) -- cgit v1.2.3