Scala reflection on function parameter names -
i have class takes function
case class functionparser1arg[t, u](func:(t => u)) def testfunc(name1:string):string = name1 val res = functionparser1arg(testfunc)
i know type signature information on function inside case class. want know both parameter name , type. have had success in finding type using runtime mirror objects, not name. suggestions?
ok, let's got symbol instance func
points to:
import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentmirror => m} val im = m reflect res.func // instance mirror
you can apply
method type members:
val apply = newtermname("apply") val applysymbol = im.symbol.typesignature member apply
and since know it's method, make method symbol:
val applymethod = applysymbol.asmethod
it's parameters can found through paramss
, , know there's 1 parameter on 1 parameter list, can first parameter of first parameter list:
val param = applymethod.paramss(0)(0)
then asking is:
val name = param.name.decoded // if want "+" instead of "$plus", example val type = param.typesignature
it's possible think that's wrong answer because got x$1
instead of name1
, passed constructor is not named function testfunc
, but, instead, anonymous function representing method created through process called eta expansion. can't find out parameter name of method because can't pass method.
if that's need, suggest use macro instead. macro, you'll able see being passed @ compile time , name it.
Comments
Post a Comment