samedi 5 septembre 2015

How to get Scala function's parameters / return type?

I have a function, and would like to obtain its parameter types and return type for use in Scala macros.

scala> val fn = (a: String, b: Double) => 123
fn: (String, Double) => Int = <function2>

scala> fn.getClass
res1: Class[_ <: (String, Double) => Int] = class $anonfun$1

In the above example, the parameter types and return type already get printed at both lines, but I don't know how to access them. Even with toString I'd be stuck with the <function2> and class $anonfun$1 parts right of the = sign -- otherwise a bit of ugly string parsing might have done.

I found that the MethodSymbolApi offers a way to extract this information for methods, but it seems like this might not help for this particular case.

I'm currently looking into AST parsing (as part of scala.meta) to extract the information, but I'd think this question would seem basic enough to be covered by the standard reflection library, though I've failed to find what I want in there. Any ideas?





Aucun commentaire:

Enregistrer un commentaire