diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-02-18 12:46:21 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-02-18 12:46:21 -0800 |
commit | bff86367607a713288a939639a654802349d1067 (patch) | |
tree | 228d886551855645d8490b7c13feca02e305db8e /src/library | |
parent | 788f38871ca52bc22967dca22355f6a1affb4e06 (diff) | |
download | scala-bff86367607a713288a939639a654802349d1067.tar.gz scala-bff86367607a713288a939639a654802349d1067.tar.bz2 scala-bff86367607a713288a939639a654802349d1067.zip |
Use if/then/else to avoid box(unbox(_))
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/reflect/ClassTag.scala | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/src/library/scala/reflect/ClassTag.scala b/src/library/scala/reflect/ClassTag.scala index b9b4772870..9dd96183da 100644 --- a/src/library/scala/reflect/ClassTag.scala +++ b/src/library/scala/reflect/ClassTag.scala @@ -69,21 +69,20 @@ trait ClassTag[T] extends ClassManifestDeprecatedApis[T] with Equals with Serial * `SomeExtractor(...)` is turned into `ct(SomeExtractor(...))` if `T` in `SomeExtractor.unapply(x: T)` * is uncheckable, but we have an instance of `ClassTag[T]`. */ - def unapply(x: Any): Option[T] = x match { - case null => None - case x: Byte => unapplyImpl(x, classOf[Byte]) // erases to: if (x instanceof Byte) unapplyImpl(BoxesRunTime.boxToByte(BoxesRunTime.unboxToByte(x)), Byte.TYPE) - case x: Short => unapplyImpl(x, classOf[Short]) - case x: Char => unapplyImpl(x, classOf[Char]) - case x: Int => unapplyImpl(x, classOf[Int]) - case x: Long => unapplyImpl(x, classOf[Long]) - case x: Float => unapplyImpl(x, classOf[Float]) - case x: Double => unapplyImpl(x, classOf[Double]) - case x: Boolean => unapplyImpl(x, classOf[Boolean]) - case x: Unit => unapplyImpl(x, classOf[Unit]) - // TODO: move this next case up and remove the redundant check in unapplyImpl? - case _ if runtimeClass isInstance x => Some(x.asInstanceOf[T]) - case _ => None - } + def unapply(x: Any): Option[T] = + if (null != x && ( + (runtimeClass.isInstance(x)) + || (x.isInstanceOf[Byte] && runtimeClass.isAssignableFrom(classOf[Byte])) + || (x.isInstanceOf[Short] && runtimeClass.isAssignableFrom(classOf[Short])) + || (x.isInstanceOf[Char] && runtimeClass.isAssignableFrom(classOf[Char])) + || (x.isInstanceOf[Int] && runtimeClass.isAssignableFrom(classOf[Int])) + || (x.isInstanceOf[Long] && runtimeClass.isAssignableFrom(classOf[Long])) + || (x.isInstanceOf[Float] && runtimeClass.isAssignableFrom(classOf[Float])) + || (x.isInstanceOf[Double] && runtimeClass.isAssignableFrom(classOf[Double])) + || (x.isInstanceOf[Boolean] && runtimeClass.isAssignableFrom(classOf[Boolean])) + || (x.isInstanceOf[Unit] && runtimeClass.isAssignableFrom(classOf[Unit]))) + ) Some(x.asInstanceOf[T]) + else None // TODO: deprecate overloads in 2.12.0, remove in 2.13.0 def unapply(x: Byte) : Option[T] = unapplyImpl(x, classOf[Byte]) |