How to get the class of an object at runtime?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to get the class of an object at runtime?

Fred Janon
Hi,

What's the way to get the class of an object at runtime? I know how to get the class of a type at compile time:

Predef:
/* Return the runtime representation of a class type. */
def classOf[T]: Class = null // this is a dummy, classOf is handled by compiler.


But when I try:

    val int: Int = 10
    println("int class: " + int.getClass())

I get this compiler error:

test.scala:16: error: type mismatch;
 found   : Int
 required: ?{val getClass: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method int2Integer in object Predef of type (Int)java.lang.Integer
 and method intWrapper in object Predef of type (Int)scala.runtime.RichInt
 are possible conversion functions from Int to ?{val getClass: ?}
        println("next: " + next + " " + int.getClass())

Thanks

Fred

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the class of an object at runtime?

Zemian Deng
Fred, you do use getClass method on "object" to get it. But your example is tricky b/c you used Int.
 
An Int in scala will be translate into Java primative, which is not an object and thus has no getClass method, but Scala will try any defined implicit converter methods before giving up. So it will look for implicit method that accepts Int, and there two defined (int->Integer, and int->RichInt), which both result has a getClass method, and so compiler complains that it can't choose one. If you explicitly decare one, it will work:
 
val n: java.lang.Integer = 10
println(n.getClass)
 
Just as you create any other AnyRef/Object, it works too:
val d = new java.util.Date
pritnln(d.getClass)
 
Hope this helps.
On Fri, Apr 25, 2008 at 10:56 PM, Fred Janon <[hidden email]> wrote:
Hi,

What's the way to get the class of an object at runtime? I know how to get the class of a type at compile time:

Predef:
/* Return the runtime representation of a class type. */
def classOf[T]: Class = null // this is a dummy, classOf is handled by compiler.


But when I try:

    val int: Int = 10
    println("int class: " + int.getClass())

I get this compiler error:

test.scala:16: error: type mismatch;
 found   : Int
 required: ?{val getClass: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method int2Integer in object Predef of type (Int)java.lang.Integer
 and method intWrapper in object Predef of type (Int)scala.runtime.RichInt
 are possible conversion functions from Int to ?{val getClass: ?}
        println("next: " + next + " " + int.getClass())

Thanks

Fred




--
Thanks,
Zemian Deng
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the class of an object at runtime?

Fred Janon
Thanks for the explanation, Zemian. I read somewhere that scala would generate a primitive type in the bytecode but it didn't ring a bell in my example.

Fred

On Sat, Apr 26, 2008 at 12:52 PM, Zemian Deng <[hidden email]> wrote:
Fred, you do use getClass method on "object" to get it. But your example is tricky b/c you used Int.
 
An Int in scala will be translate into Java primative, which is not an object and thus has no getClass method, but Scala will try any defined implicit converter methods before giving up. So it will look for implicit method that accepts Int, and there two defined (int->Integer, and int->RichInt), which both result has a getClass method, and so compiler complains that it can't choose one. If you explicitly decare one, it will work:
 
val n: java.lang.Integer = 10
println(n.getClass)
 
Just as you create any other AnyRef/Object, it works too:
val d = new java.util.Date
pritnln(d.getClass)
 
Hope this helps.
On Fri, Apr 25, 2008 at 10:56 PM, Fred Janon <[hidden email]> wrote:
Hi,

What's the way to get the class of an object at runtime? I know how to get the class of a type at compile time:

Predef:
/* Return the runtime representation of a class type. */
def classOf[T]: Class = null // this is a dummy, classOf is handled by compiler.


But when I try:

    val int: Int = 10
    println("int class: " + int.getClass())

I get this compiler error:

test.scala:16: error: type mismatch;
 found   : Int
 required: ?{val getClass: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method int2Integer in object Predef of type (Int)java.lang.Integer
 and method intWrapper in object Predef of type (Int)scala.runtime.RichInt
 are possible conversion functions from Int to ?{val getClass: ?}
        println("next: " + next + " " + int.getClass())

Thanks

Fred




--
Thanks,
Zemian Deng

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get the class of an object at runtime?

kishorekumar522
This post has NOT been accepted by the mailing list yet.
Can try using

def objectToString[T](obj:T):String = obj.getClass.getSimpleName.replace("$", "")
Loading...