diff options
author | Sébastien Doeraene <sjrdoeraene@gmail.com> | 2016-04-13 14:55:02 +0200 |
---|---|---|
committer | Sébastien Doeraene <sjrdoeraene@gmail.com> | 2016-04-13 16:20:46 +0200 |
commit | 8fc0516d6deeb84af9bac5370f1d21511e0f86d6 (patch) | |
tree | 74ee8fdc85c8c7fc8e8e21b954a34e8781044475 /src/library | |
parent | 51996c8474a06656f8c21302af2b4151bdc2393e (diff) | |
download | scala-8fc0516d6deeb84af9bac5370f1d21511e0f86d6.tar.gz scala-8fc0516d6deeb84af9bac5370f1d21511e0f86d6.tar.bz2 scala-8fc0516d6deeb84af9bac5370f1d21511e0f86d6.zip |
Inline ScalaRunTime.arrayElementClass at call sites.
This method was awful. Not only it was using run-time type
tests to essentially encode compile-time overloading. But
it also did 2 slightly different things for the Class case
and ClassTag case.
All in all, it is much more readable to inline the
appropriate implementation at every call site.
Diffstat (limited to 'src/library')
5 files changed, 10 insertions, 23 deletions
diff --git a/src/library/scala/collection/mutable/ArrayOps.scala b/src/library/scala/collection/mutable/ArrayOps.scala index b7682c5ab9..507585b9cf 100644 --- a/src/library/scala/collection/mutable/ArrayOps.scala +++ b/src/library/scala/collection/mutable/ArrayOps.scala @@ -11,7 +11,6 @@ package collection package mutable import scala.reflect.ClassTag -import scala.runtime.ScalaRunTime._ import parallel.mutable.ParArray /** This class serves as a wrapper for `Array`s with all the operations found in @@ -35,7 +34,7 @@ import parallel.mutable.ParArray sealed trait ArrayOps[T] extends Any with ArrayLike[T, Array[T]] with CustomParallelizable[T, ParArray[T]] { private def elementClass: Class[_] = - arrayElementClass(repr.getClass) + repr.getClass.getComponentType override def copyToArray[U >: T](xs: Array[U], start: Int, len: Int) { val l = len min repr.length min (xs.length - start) @@ -43,7 +42,7 @@ sealed trait ArrayOps[T] extends Any with ArrayLike[T, Array[T]] with CustomPara } override def toArray[U >: T : ClassTag]: Array[U] = { - val thatElementClass = arrayElementClass(implicitly[ClassTag[U]]) + val thatElementClass = implicitly[ClassTag[U]].runtimeClass if (elementClass eq thatElementClass) repr.asInstanceOf[Array[U]] else @@ -91,7 +90,7 @@ sealed trait ArrayOps[T] extends Any with ArrayLike[T, Array[T]] with CustomPara val bb: Builder[Array[U], Array[Array[U]]] = Array.newBuilder(ClassTag[Array[U]](elementClass)) if (isEmpty) bb.result() else { - def mkRowBuilder() = Array.newBuilder(ClassTag[U](arrayElementClass(elementClass))) + def mkRowBuilder() = Array.newBuilder(ClassTag[U](elementClass.getComponentType)) val bs = asArray(head) map (_ => mkRowBuilder()) for (xs <- this) { var i = 0 @@ -184,7 +183,7 @@ object ArrayOps { override protected[this] def thisCollection: WrappedArray[T] = new WrappedArray.ofRef[T](repr) override protected[this] def toCollection(repr: Array[T]): WrappedArray[T] = new WrappedArray.ofRef[T](repr) - override protected[this] def newBuilder = new ArrayBuilder.ofRef[T]()(ClassTag[T](arrayElementClass(repr.getClass))) + override protected[this] def newBuilder = new ArrayBuilder.ofRef[T]()(ClassTag[T](repr.getClass.getComponentType)) def length: Int = repr.length def apply(index: Int): T = repr(index) diff --git a/src/library/scala/collection/mutable/WrappedArray.scala b/src/library/scala/collection/mutable/WrappedArray.scala index 8740bda835..01dcd9bde5 100644 --- a/src/library/scala/collection/mutable/WrappedArray.scala +++ b/src/library/scala/collection/mutable/WrappedArray.scala @@ -13,7 +13,6 @@ package collection package mutable import scala.reflect.ClassTag -import scala.runtime.ScalaRunTime._ import scala.collection.generic._ import scala.collection.parallel.mutable.ParArray @@ -46,7 +45,7 @@ extends AbstractSeq[T] def elemTag: ClassTag[T] @deprecated("use elemTag instead", "2.10.0") - def elemManifest: ClassManifest[T] = ClassManifest.fromClass[T](arrayElementClass(elemTag).asInstanceOf[Class[T]]) + def elemManifest: ClassManifest[T] = ClassManifest.fromClass[T](elemTag.runtimeClass.asInstanceOf[Class[T]]) /** The length of the array */ def length: Int @@ -63,10 +62,10 @@ extends AbstractSeq[T] override def par = ParArray.handoff(array) private def elementClass: Class[_] = - arrayElementClass(array.getClass) + array.getClass.getComponentType override def toArray[U >: T : ClassTag]: Array[U] = { - val thatElementClass = arrayElementClass(implicitly[ClassTag[U]]) + val thatElementClass = implicitly[ClassTag[U]].runtimeClass if (elementClass eq thatElementClass) array.asInstanceOf[Array[U]] else @@ -122,7 +121,7 @@ object WrappedArray { def newBuilder[A]: Builder[A, IndexedSeq[A]] = new ArrayBuffer final class ofRef[T <: AnyRef](val array: Array[T]) extends WrappedArray[T] with Serializable { - lazy val elemTag = ClassTag[T](arrayElementClass(array.getClass)) + lazy val elemTag = ClassTag[T](array.getClass.getComponentType) def length: Int = array.length def apply(index: Int): T = array(index).asInstanceOf[T] def update(index: Int, elem: T) { array(index) = elem } diff --git a/src/library/scala/collection/mutable/WrappedArrayBuilder.scala b/src/library/scala/collection/mutable/WrappedArrayBuilder.scala index c4781321d7..5bc5811450 100644 --- a/src/library/scala/collection/mutable/WrappedArrayBuilder.scala +++ b/src/library/scala/collection/mutable/WrappedArrayBuilder.scala @@ -13,7 +13,6 @@ package collection package mutable import scala.reflect.ClassTag -import scala.runtime.ScalaRunTime._ /** A builder class for arrays. * @@ -34,7 +33,7 @@ class WrappedArrayBuilder[A](tag: ClassTag[A]) extends ReusableBuilder[A, Wrappe private var size: Int = 0 private def mkArray(size: Int): WrappedArray[A] = { - val runtimeClass = arrayElementClass(tag) + val runtimeClass = tag.runtimeClass val newelems = runtimeClass match { case java.lang.Byte.TYPE => new WrappedArray.ofByte(new Array[Byte](size)).asInstanceOf[WrappedArray[A]] case java.lang.Short.TYPE => new WrappedArray.ofShort(new Array[Short](size)).asInstanceOf[WrappedArray[A]] diff --git a/src/library/scala/reflect/ClassTag.scala b/src/library/scala/reflect/ClassTag.scala index 9dd96183da..7f037ce17b 100644 --- a/src/library/scala/reflect/ClassTag.scala +++ b/src/library/scala/reflect/ClassTag.scala @@ -2,7 +2,6 @@ package scala package reflect import java.lang.{ Class => jClass } -import scala.runtime.ScalaRunTime.arrayElementClass /** * @@ -105,7 +104,7 @@ trait ClassTag[T] extends ClassManifestDeprecatedApis[T] with Equals with Serial override def hashCode = scala.runtime.ScalaRunTime.hash(runtimeClass) override def toString = { def prettyprint(clazz: jClass[_]): String = - if (clazz.isArray) s"Array[${prettyprint(arrayElementClass(clazz))}]" else + if (clazz.isArray) s"Array[${prettyprint(clazz.getComponentType)}]" else clazz.getName prettyprint(runtimeClass) } diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index 71a3603a42..151d4394bb 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -43,15 +43,6 @@ object ScalaRunTime { else java.lang.reflect.Array.newInstance(clazz, 0).getClass } - /** Return the class object representing elements in arrays described by a given schematic. - */ - def arrayElementClass(schematic: Any): jClass[_] = schematic match { - case cls: jClass[_] => cls.getComponentType - case tag: ClassTag[_] => tag.runtimeClass - case _ => - throw new UnsupportedOperationException(s"unsupported schematic $schematic (${schematic.getClass})") - } - /** Return the class object representing an unboxed value type, * e.g., classOf[int], not classOf[java.lang.Integer]. The compiler * rewrites expressions like 5.getClass to come here. |