I'm trying to invoke a function using the scala reflection api in v2.11.6:
import scala.reflect.runtime.{universe => ru}
def f(i: Int) = i + 2
val fn: Any = (f _)
val ref = ru.runtimeMirror(ru.getClass.getClassLoader).reflect(fn)
val apply = ref.symbol.typeSignature.member(ru.TermName("apply"))
When using ref.reflectMethod(apply.asMethod)
it complains about multiple alternatives to apply
on ref
. Examining apply.asTerm.alternatives
reveals two methods, one with signature (x$1: Int)Int
and the other with (v1: T1)R
. Calling
ref.reflectMethod(apply.asTerm.alternatives(1).asInstanceOf[ru.MethodSymbol])(1)
(with the second alternative) returns the correct result (3). However calling the first alternative raises an exception: java.lang.IllegalArgumentException: object is not an instance of declaring class
What are those alternatives and how can I make sure to always invoke the proper one?
Aucun commentaire:
Enregistrer un commentaire