1

I have a function/method that receives objects (like def fun(t : Object)). Now if someone creates a class and calls my function using an object, is there a way of finding out which class it came from?

For example a pseudo-code

class Test {         
   def sum() {} // some arbitrary method
}

def fun(t : Object) {
   val ob = t.asInstanceOf[Test]
   ob.sum() // this will work if I know the classname (Test) 
}

but if the user creates a new class and sends me the object how am I supposed to typecast it and access the objects? Is there a way to find the class type using the given object? I tried to call t.getClass() but it is not working for me. Kindly help me out !!!

0__
  • 66,707
  • 21
  • 171
  • 266
Rags
  • 434
  • 2
  • 6
  • 20
  • have you tried `t.getClass()`? – gobernador Jul 20 '12 at 17:37
  • Why are you not using interface for this purpose? – JProgrammer Jul 20 '12 at 17:38
  • yes i tried .. when i do say var func = t.asInstanceof[t.getclass()] its giving lot f errors – Rags Jul 20 '12 at 17:44
  • I m not sure how to use interface for this purpose . I m using scala traits and the class test ll extend from that trait. I m not sure how to work with interface – Rags Jul 20 '12 at 17:46
  • 1
    This looks similar to your problem: http://stackoverflow.com/questions/3039822/how-do-i-call-a-scala-object-method-using-reflection?rq=1 – Brian Jul 20 '12 at 19:28
  • 4
    On the answer that was deleted you added *a complete new requirement*: that you don't know what `test` is at compile time. If you don't know what `test` is at compile time, why do you think it will have a `sum` method? It seems there's a lot missing in this question. As stated, the deleted answer was just fine. – Daniel C. Sobral Jul 20 '12 at 22:20
  • As Daniel said, you will need to be more precise with your question. What is the purpose of your "function" - are you trying to call a particular method, independent of the type passed in? E.g. are you trying to recover a method only knowing its name and argument types (that would be a structural type)? What do you mean by "the user creates a new class" - are you assuming dynamic class loading? – 0__ Jul 21 '12 at 01:00
  • You might want to recover a structural type, which isn't that easy, so you probably need plain old reflection: http://stackoverflow.com/questions/1988181/pattern-matching-structural-types-in-scala – 0__ Jul 21 '12 at 01:09

1 Answers1

0

You can use pattern matching to match on the objects you receiving in the function. When you match on a case class then you have access to the functions of that class and can call them. In the example below, you can call functions on f or b in the respective match. e.g. f.toString.

scala> def f(o: Object) = o match{
 | case f:Foo => println("o is a Foo")
 | case b:Bar => println("o is a Bar")
 | case _   => println("o is unknown")
 | }
Brian
  • 20,195
  • 6
  • 34
  • 55
  • Yes for all this to work you need to know that we have a class named foo or Bar. What if he creates a class named BALL. So no way of finding the class . I wanted to figure out a way to do it without hard coding it .. – Rags Jul 20 '12 at 17:57
  • 1
    Then you would have to use reflection on the runtime class to do that. – Brian Jul 20 '12 at 18:38