On 2012-05-08 15:37, Miles Sabin wrote:
> On Tue, May 8, 2012 at 2:34 PM, Stefan Zeiger<
sze...@novocode.com> wrote:
>> Actually, I think it wouldn't (but I only realized that after posting).
>> There's not really a need to do call-by-name if you get the AST anyway. What
>> I posted above should work fine with a plain f: Unit parameter. But it still
>> runs into the same scoping issue as the original version.
> True, but injecting a value into the argument via an implicit has to
> be easier for the macro writer than doing it via a tree rewrite, no?
I think it's pretty much the only way to do it without untyped macros
(if the scoping worked as expected) but by-name parameters would only
create some unnecessary overhead. This feature would be ideal for
statically scoped session handling. Currently we have this in SLICK:
def withSession[T](f: Session => T): T = {
val s = createSession()
try { f(s) } finally s.close()
}
def withSession[T](f: => T): T = withSession { s: Session =>
Database.dyn.withValue(s)(f) }
Either pass around the session value (or make your your own implicit
value for it), or rely on a dynamically scoped thread-local session.
With macros, we could do:
def withSession[T](f: T) = macro withSession_impl
def withSession_impl[T : TypeTag](c : Context)(f: c.Expr[T]) = c.reify{
c.prefix.eval.withSession[T] { s =>
implicit val session = s
f.eval
}
}
OTOH, implicit values affect type-checking, so maybe that's a lost cause
unless we get untyped macros.
-sz