mardi 21 juillet 2020

Invoke method based on list in scala

I have object in scala, where I have defined functions. I have Seq where all the functions are listed as the elements as below:

object ABC {

  def abc(a: Int, b: Int): Int = a+b
  def pqr(p: Int, q: Int): Int = p - q
  val listFunction: Seq[(Int, Int) => Int] = Seq(abc, pqr)


}

Now, I want to call the list which I defined as string and execute all the functions inside it, by taking the parameters. Similar to the below:

val listName = "listFunction"
val method = example.ABC.getClass.getMethod(listName,1,2)

Can someone help me, how to invoke the listFunction list and execute all the functions inside it.





Aucun commentaire:

Enregistrer un commentaire