diff options
author | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-09-06 17:24:17 -0700 |
---|---|---|
committer | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-09-06 17:24:17 -0700 |
commit | b7e08723d142c8227181eed283e7c982f449425a (patch) | |
tree | 78c7a93464bca87cf9e18c82c160a80dc0770947 /src | |
parent | bc8a7c81d2050755468ff25af3b8cb33a3b4108a (diff) | |
parent | 6a740332c7bfd56b20993be6ecd0bf818104f56c (diff) | |
download | scala-b7e08723d142c8227181eed283e7c982f449425a.tar.gz scala-b7e08723d142c8227181eed283e7c982f449425a.tar.bz2 scala-b7e08723d142c8227181eed283e7c982f449425a.zip |
Merge pull request #1265 from scalamacros/ticket/6318
SI-6318 fixes ClassTag.unapply for primitives
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/reflect/ClassTag.scala | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/library/scala/reflect/ClassTag.scala b/src/library/scala/reflect/ClassTag.scala index aaef959d7c..e42c89d21f 100644 --- a/src/library/scala/reflect/ClassTag.scala +++ b/src/library/scala/reflect/ClassTag.scala @@ -54,7 +54,26 @@ 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] = if (x != null && runtimeClass.isAssignableFrom(x.getClass)) Some(x.asInstanceOf[T]) else None + def unapply(x: Any): Option[T] = unapply_impl(x) + def unapply(x: Byte): Option[T] = unapply_impl(x) + def unapply(x: Short): Option[T] = unapply_impl(x) + def unapply(x: Char): Option[T] = unapply_impl(x) + def unapply(x: Int): Option[T] = unapply_impl(x) + def unapply(x: Long): Option[T] = unapply_impl(x) + def unapply(x: Float): Option[T] = unapply_impl(x) + def unapply(x: Double): Option[T] = unapply_impl(x) + def unapply(x: Boolean): Option[T] = unapply_impl(x) + def unapply(x: Unit): Option[T] = unapply_impl(x) + + private def unapply_impl[U: ClassTag](x: U): Option[T] = + if (x == null) None + else { + val staticClass = classTag[U].runtimeClass + val dynamicClass = x.getClass + val effectiveClass = if (staticClass.isPrimitive) staticClass else dynamicClass + val conforms = runtimeClass.isAssignableFrom(effectiveClass) + if (conforms) Some(x.asInstanceOf[T]) else None + } /** case class accessories */ override def canEqual(x: Any) = x.isInstanceOf[ClassTag[_]] |