Resolving variables in scope modified by Scala macro -
i'm investigating possibility make following kind of imports compile in scala:
def helpers[t] = new { def foo: t = ??? } import helpers[int]._
this arose idea pin down types scala not infer on own. manually expanded version works:
val m = helpers[int] import m._ (foo: int)
however, naive attempt @ macro failed:
import scala.reflect.macros.blackbox.context object open { def open[t](m: any)(t: t): t = macro open_impl[t] def open_impl[t](c: context)(m: c.tree)(t: c.tree): c.tree = { import c.universe._ val freshname = termname(c.freshname("import$")) q"{ val $freshname = $m; import $freshname._; $t }" } }
the syntax had in mind let open m in
-syntax ocaml. problem being binding of variables seems happen before macro expansion:
import open._ object foo { val x = 7 } open(foo) { println(x) }
fails in repl with:
<console>:16: error: not found: value x open(foo) { println(x) }
any ideas how work around this? @ possible macros? or perhaps compiler plugin?
Comments
Post a Comment