scala - How to pass arguments to a function of arbitrary number of parameters via macro? -
i have deconstructed function, got vparams, , able call:
case class action(f: function, ts: list[typename]) { def render(rulename: string): expr[unit] = c.expr[unit](q""" val p = ${c.prefix} val value1 = p.valuestack.pop().asinstanceof[${ts(0)}] val value2 = p.valuestack.pop().asinstanceof[${ts(1)}] p.valuestack.push($f(value1, value2)) """) }
f of arbitrary number of parameters known @ compile time.
how pass ts.count parameters p.valuestack function f?
how this:
case class action(f: function, ts: list[typename]) { def render(rulename: string): expr[unit] = { val argnames = (1 ts.size).map(i => newtermname(s"value$i")) val args = argnames.map(name => ident(name)) val valdefs = (ts zip argnames).map { case (tn, argname) => q"val $argname = p.valuestack.pop().asinstanceof[$tn]" } c.expr[unit]( q""" val p = ${c.prefix} ..$valdefs p.valuestack.push($f(..$args)) """) } }
this compiles (in scala 2.10.2 macro paradise plugin), didn't test whether works.
Comments
Post a Comment