Domanda

There is a method with this signature that creates implicit parameter behind the scene:

class A {
    def test[T: ClassTag](t: T) = println(t)
}
val a = new A
a.test(123456)

So the implicit parameter should be provided to the call of method test. When I do the reify in REPL, call tree looks like this:

Apply(
    Apply(
        Select(
            Select(
                newTermName("a"), 
                newTermName("test")
            ), 
        List(
            Literal(Constant(123456))) // explicit method args
        ), 
    List(
        Select(This(newTypeName("Predef")),
        newTermName("implicitly"))) // implicit args
    )
)

I'm trying to make this call using macro implementation but this doesn't compile and gives me: Predef is not an enclosing class. I tried different things without success and didn't found any sample on this.

So the question is: how to provide implicit argument to method using macro?

È stato utile?

Soluzione

Manual AST manipulation can be tricky. Try Select(Ident(newTermName("scala")), newTermName("Predef")) instead of This(newTypeName("Predef")).

Or even better, consider using quasiquotes (http://docs.scala-lang.org/overviews/macros/quasiquotes.html). With them, composing a reference to implicitly becomes as simple as scala.Predef.implicitly.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top