Reputation: 2605
I have to write scheduler that callbacks a function after processing. I can register the class name and method name as strings and have to callback it in future.
How can I call a function with class name and method name in Scala.
Upvotes: 1
Views: 686
Reputation: 31574
Same with java, use reflect.
package tst
class A {
def fun(a:String) = print(a)
}
object B extends App {
val classA = Class.forName("tst.A")
val method = classA.getDeclaredMethod("fun", classOf[String])
method.invoke(classA.newInstance(), "hello world")
}
Upvotes: 2