- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
def runtimeClass
Tue, 2011-06-14, 22:27
The opening of SI-4696 reminded me I have this lying around. What do we think about it.
https://github.com/paulp/scala-dev/tree/runtime-class
Summarized via scaladoc:
/** This class exists to install the runtimeClass() method on any target.
* It improves on Object.getClass (as seen from scala) in the following ways:
*
* - it correctly returns classOf[Int], not classOf[Integer], if called on an Int.
* - it can be used on Any and AnyVal, whereas getClass only exists on AnyRef.
* - the return type is a more precise Class[_ <: T], where T is the static type of the target.
*
* The return type should be consistent with getClass as seen from java. According
* to the javadoc, "The actual result type [of getClass] is Class<? extends |X|> where
* |X| is the erasure of the static type of the expression on which getClass is called."
{{{
object RuntimeClassVsGetClass {
class A
class B extends A
// scala expressions, with the expression's type commented:
(new B).getClass().newInstance() // Any
(new B).runtimeClass().newInstance() // B
(new B: A).runtimeClass().newInstance() // A
5.getClass // doesn't compile
5.asInstanceOf[AnyRef].getClass // classOf[_ <: java.lang.Integer]
5.runtimeClass() // classOf[_ <: Int] (i.e. java.lang.Integer.TYPE or classOf[Int])
(5: AnyVal).runtimeClass() // classOf[_ <: java.lang.Integer] (AnyVal implies boxed)
(5: Any).runtimeClass() // classOf[_ <: java.lang.Integer] (as does Any)
}
}}}
Wed, 2011-06-15, 00:17
#2
Re: def runtimeClass
On Tue, Jun 14, 2011 at 10:27 PM, Paul Phillips wrote:
> The opening of SI-4696 reminded me I have this lying around. What do we think about it.
>
> https://github.com/paulp/scala-dev/tree/runtime-class
>
> Summarized via scaladoc:
Yes, that would be very useful.
Cheers,
Miles
Thu, 2011-06-16, 22:17
#3
Re: def runtimeClass
On Tue, Jun 14, 2011 at 5:27 PM, Paul Phillips wrote:
> https://github.com/paulp/scala-dev/tree/runtime-class
> /** This class exists to install the runtimeClass() method on any target.
> * It improves on Object.getClass (as seen from scala) in the following ways:
> *
> * - it correctly returns classOf[Int], not classOf[Integer], if called on an Int.
> * - it can be used on Any and AnyVal, whereas getClass only exists on AnyRef.
> * - the return type is a more precise Class[_ <: T], where T is the static type of the target.
DO WANT
This is our old friend #6e6e6e,
https://issues.scala-lang.org/browse/SI-490 . I know the
number by heart because I've typed the comment "// remove cast once
#6e6e6e is fixed"
many times.
A little devil on my shoulder tells me it should have a cool two
character name like ##
or ==. Silence, devil!
Thu, 2011-06-16, 22:47
#4
Re: def runtimeClass
Le 16/06/2011 23:07, Seth Tisue a écrit :
>
> A little devil on my shoulder tells me it should have a cool two
> character name like ##
> or ==. Silence, devil!
That's harder than for hash or equal, because I don't know of any symbol
whose name is said "runtime" or "class".
But my keyboard is full of nice things, like ~~ (because it's so cool to
have that feature), ¤¤ or §§. And there is classics like @@, $$ and
certainly others.
Cheers,
I think it would be nice. And something like that has been requested a few times before, I believe.
Best,Ismael