Consider following code snippet:
object Example {
def run(f: => Unit): Unit = {
implicit val i = 1
f
}
def caller(): Unit =
run {
todo
}
def todo(implicit i: Int): Unit =
println(i)
}
which currently is not compiling with following message:
Error:(14, 13) could not find implicit value for parameter i: Int
todo
^
My question is it possible to make implicit parameter available to call-by-name function body?
EDIT I tried make it working with macro implementation as suggested by Alexey Romanov
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
object Macros {
def run(f: => Unit): Unit = macro runImpl
def runImpl(c : Context)(f: c.Tree) = {
import c.universe._
q"""{
implicit val i: Int = 3
$f
}"""
}
}
object Example extends App {
Macros.run {
todo
}
def todo(implicit i: Int): Unit =
println(i)
}
Debugging macro i can see that it is correctly expanded into
{
implicit val i: Int = 3
Example.this.todo
}
Unfortunately it does not compiles as well with same error that implicit is not found.
Digging into issue i found discussions here and jira issues https://issues.scala-lang.org/browse/SI-5774
So question is the same: Is it possible to tunnel implicit into todo
function in this case?