diff options
author | Paul Phillips <paulp@improving.org> | 2011-06-22 21:10:33 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-06-22 21:10:33 +0000 |
commit | 9853b5b8292650d8e192b500e636dc441550eef7 (patch) | |
tree | ec3a85b9b7af29c9fbd3bba17389791f083394cc /src/library/scala/runtime/ScalaRunTime.scala | |
parent | e49ec10e93eeec1af775096de07dd46a7d0a1fad (diff) | |
download | scala-9853b5b8292650d8e192b500e636dc441550eef7.tar.gz scala-9853b5b8292650d8e192b500e636dc441550eef7.tar.bz2 scala-9853b5b8292650d8e192b500e636dc441550eef7.zip |
A total rewrite of "runtimeClass", discarding t...
A total rewrite of "runtimeClass", discarding the user-space approach in
favor of simply fixing getClass.
def f1 = 5.getClass // Class[Int]
def f2 = (5: AnyVal).getClass // Class[_ <: AnyVal]
def f3 = (5: java.lang.Integer).getClass // Class[_ <: java.lang.Integer]
class A
class B extends A
def f1 = (new B: Any).getClass().newInstance() // Any
def f2 = (new B: AnyRef).getClass().newInstance() // AnyRef
def f3 = (new B: A).getClass().newInstance() // A
def f4 = (new B: B).getClass().newInstance() // B
But that's not all!
def f0[T >: B] = (new B: T).getClass().newInstance()
def f5 = f0[Any] // Any
def f6 = f0[AnyRef] // AnyRef
def f7 = f0[A] // A
def f8 = f0[B] // B
Closes #490, #896, #4696. Review by moors. (Note: I think this is pretty
good, but picky review requested.)
Diffstat (limited to 'src/library/scala/runtime/ScalaRunTime.scala')
-rw-r--r-- | src/library/scala/runtime/ScalaRunTime.scala | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index 7a8224298b..031f15f10a 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -32,6 +32,22 @@ object ScalaRunTime { def isValueClass(clazz: Class[_]) = clazz.isPrimitive() + /** 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. + */ + def anyValClass[T <: AnyVal](value: T): Class[T] = (value match { + case x: Byte => java.lang.Byte.TYPE + case x: Short => java.lang.Short.TYPE + case x: Char => java.lang.Character.TYPE + case x: Int => java.lang.Integer.TYPE + case x: Long => java.lang.Long.TYPE + case x: Float => java.lang.Float.TYPE + case x: Double => java.lang.Double.TYPE + case x: Boolean => java.lang.Boolean.TYPE + case x: Unit => java.lang.Void.TYPE + }).asInstanceOf[Class[T]] + /** Retrieve generic array element */ def array_apply(xs: AnyRef, idx: Int): Any = xs match { case x: Array[AnyRef] => x(idx).asInstanceOf[Any] |