I am trying to call a function using a variable as String type dynamically i.e. variable will contain a function name as String. So, I need to call a function using that variable.
So, I am using Scala Reflection. It is working if the function is accepting datatype as Sting, But it is throwing error List[Map[String, Double]]
I used below link to refer a code
Is there any Scala feature that allows you to call a method whose name is stored in a string?
and below is a test program which I am trying to test
package test
import scala.collection.immutable.Map
class DynamicaVariable {
def cat(s1: List[Map[String, Double]]) = {
println(s1)
}
}
object DynamicaVariable{
def main(args: Array[String]): Unit = {
val obj = new DynamicaVariable
val data = List(
Map("a" -> 1.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0), Map("a" -> 1.0, "b" -> 2678.0, "c" -> 40.0, "d" -> 2.0), Map("a" -> 4.0, "b" -> 267.0, "c" -> 26.0, "d" -> 2.0), Map("a" -> 1.0, "b" -> 2678.0, "c" -> 90.0, "d" -> 17.0)
)
val functionName = "cat"
val method = obj.getClass.getMethod(functionName,data.getClass)
method.invoke(obj,data)
}
}
My scala version is 2.11.6 and I am using IntelliJ. I have checked the SDK version also. And unchecked the "Run worksheet in the compile process" under scala
But still no luck! Any idea will be helpful. Thanks!