I wrote,
> compiles and produces,
>
> TypeApply(Select(Select(This(newTypeName("foo")), foo.Foo),
> newTermName("mkFoo")), List(TypeTree(), TypeTree()))
>
> which looks like it's getting pretty much to where I want it to be :-)
Unfortunately if I then try and run this tree,
val ivb = toolbox.resetAllAttrs(iv)
val v = toolbox.runExpr(ivb)
println(v)
I get the following error at runtime,
scala.tools.reflect.ToolBoxError: reflective toolbox has failed:
cannot operate on trees that are already typed
Is there some other incantation necessary to compile a tree that's
been inferred in this way?
Alternatively, I'd actually prefer to be able to do this in a single
step rather than infer the implicit and then evaluate it. I've tried
things along the lines of,
package foo {
trait Foo[T, U]
object Foo {
implicit val is = new Foo[Int, String] {
override def toString() = "Foo[Int, String]"
}
}
}
package bar {
object Test extends App {
import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
val toolbox = currentMirror.mkToolBox()
val tree =
Apply(
TypeApply(
Select(
Select(
Ident(newTermName("scala")),
newTermName("Predef")),
newTermName("implicitly")),
List(
AppliedTypeTree(
Select(
Ident(newTermName("foo")),
newTypeName("Foo")),
List(
Select(
Ident(newTermName("scala")),
newTypeName("Int")),
Select(
Select(
Ident(newTermName("java")),
newTermName("lang")),
newTypeName("String")))))), Nil)
val rv = toolbox.runExpr(tree)
println(rv)
}
}
Here I get a runtime error,
scala.tools.reflect.ToolBoxError: reflective compilation has failed:
not enough arguments for method implicitly: (implicit e:
foo.Foo[Int,String])foo.Foo[Int,String].
Unspecified value parameter e.
So it looks as though in the inferImplicitValue case typechecking goes
too far to enable subsequent compilation and execution, and in the
second case it doesn't do enough work to resolve the implicit
arguments. Any ideas?