dimanche 14 août 2016

Scala: reflection with different argument types

I'm trying to use reflection, pretty much in the same way seen in the answer of this question. The thing is that my code looks like this:

class A {
  def query1(arg: Int): String = { Some code }
  def query2(arg: String): String = { Some code }
  def query3(): String = { Some code }
}

object A {
  def apply(query: String, arg: Any): String = {
    val a = new A
    val method = a.getClass.getMethod(query,arg.getClass)
    method.invoke(a,arg)
  }
}

But this cannot be compiled, and I get an error:

the result of an implicit conversion must be more specific than AnyRef

Any ideas how can I make this work?





Aucun commentaire:

Enregistrer un commentaire