I was playing with Scala 2.11's new macro features. I wanted to see if I could do the following rewrite:
forRange(0 to 10) { i => println(i) }
// into
val iter = (0 to 10).iterator
while (iter.hasNext) {
val i = iter.next
println(i)
}
I think I got fairly close with this macro:
def _forRange[A](c: BlackboxContext)(range: c.Expr[Range])(func: c.Expr[Int => A]): c.Expr[Unit] = {
import c.universe._
val tree = func.tree match {
case q"($i: $t) => $body" => q"""
val iter = ${range}.iterator
while (iter.hasNext) {
val $i = iter.next
$body
}
"""
case _ => q""
}
c.Expr(tree)
}
This produces the following output when called as forRange(0 to 10) { i => println(i) }
(at least, it's what the show
function gives me on the resultant tree):
{
val iter = scala.this.Predef.intWrapper(0).to(10).iterator;
while$1(){
if (iter.hasNext)
{
{
val i = iter.next;
scala.this.Predef.println(i)
};
while$1()
}
else
()
}
}
That looks like it should work, but there's a conflict between my manually defined val i
and the i
referenced in the spliced-in function body. I get the following error:
ReplGlobal.abort: symbol value i does not exist in$line38.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw. error: symbol value i does not exist in scala.reflect.internal.FatalError: symbol value i does not exist in $line38.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw.
And then a rather large stack trace, resulting in an "Abandoned crashed session" notification.
I can't tell if this is a problem with my logic (you simply can't splice in a function body that references a closed-over variable), or if it's a bug with the new implementation. The error reporting certainly could be better. It may be exacerbated by the fact that I'm running this on the Repl.
Is it possible to pull apart a function, separating the body from the closed-over terms, and rewrite it in order to splice the logic directly into a resulting tree?
resetAllAttrs
has been removed from Scala 2.11? I have the exact same problem, and I am desperate to fix it! – RedmanresetLocalAttrs
that still exists which should cover most cases. Dunno if this is one of them, but it's worth a shot. Reference: docs.scala-lang.org/overviews/macros/changelog211.html – ArcheozoicresetLocalAttrs
has been renamed tountypecheck
. Reference: github.com/scalamacros/resetallattrs – Archeozoicuntypecheck
is enough. However, when I try to match the patternq"for ($i <- $collection) $body"
, I need the oldresetAllAttrs
(or so it seems). I've used the resetAllAttrs library for Scala 2.11 macros, and it works, whereasuntypecheck
alone does not, but I quickly run into the usual problem withresetAllAttrs
corrupting parents trees. – RedmanresetAllAttrs
back, but they removed it specifically because of the tree-corrupting issue you mentioned. Still, if you need it, look into github.com/scalamacros/resetallattrs – Archeozoic