[SIP 16] Using Context.reify to generate a variable number of expressions

67 views
Skip to first unread message

Peter Niederwieser

unread,
Mar 28, 2012, 9:28:15 AM3/28/12
to scala-sips
I'm working on a project (https://github.com/pniederw/expecty) that
uses a macro to bring Groovy/Spock-style power assertions to Scala.
(I'm the original author of Groovy power asserts.) Currently, the
project is based on the Scala Macros ("Kepler") fork.

Initially, I tried to use Context.reify() as much as possible. After
all, who wants to construct ASTs by hand? I started out with something
like this:

val expr: c.Tree = ...

c.reify {
val runtime = new org.expectify.Runtime
runtime.record(expr.eval)
}

This worked fine. Next I needed to do the same for a variable number
of expressions. I tried something like:

val exprs: List[c.Tree] = ...

c.reify {
val runtime = new org.expectify.Runtime
for (expr <- exprs) runtime.record(expr.value)
}

Obviously this doesn't work because the loop needs to get executed at
macro expansion time. So I tried to use multiple reify's (one for the
val and another one for each expression) and manually combined the
resulting expressions into a Block. This resulted in a new problem,
namely that the compiler wasn't able to resolve the references to the
val anymore. In the end, the only way I was able to make this work was
to give up on reify and construct all ASTs by hand. Is there a better
way?

Cheers,
Peter

PS: Is this the right list for this kind of macro-related questions?

Eugene Burmako

unread,
Mar 29, 2012, 10:09:20 AM3/29/12
to scala-sips
Hi, yeah, scala-sips is probably the best place to discuss macros.

Speaking of your main question, unfortunately, there is no way to use
reify to solve your problem. Future developments might make reify more
powerful, but for now you have to hand-craft the ASTs.

One of the workarounds would be building the "runtime.record(...)"
application by hand, wrapping it into Expr[T] (given that your know
the T that record is going to return) and splicing that Expr into the
enclosing reify.
Reply all
Reply to author
Forward
0 new messages