Search code examples
scalaimplicitscala-macrosscala-quasiquotesscala-macro-paradise

Calling function with implicit parameter from quasioquote


For some reason every time I try to call a function with an implicit parameter from quasiquotes, it fails with Can't unquote x.universe.Tree, consider providing an implicit instance of Liftable[x.universe.Tree]

What's wrong with that? Am I not allowed to do that? I couldn't find anywhere where it says I can't do it

import scala.language.experimental.macros
import scala.reflect.macros.whitebox

object Foo {
  def foo: Unit = macro fooImpl

  def fooImpl(c: whitebox.Context): c.Expr[Unit] = {
    import c.universe._
    implicit val x = c      

    val res = q"$sysoutFQN"
    c.Expr(res)
  }

  def sysoutFQN(implicit c: whitebox.Context): c.universe.Tree = {
    import c.universe._
    q"java.lang.System.out.println()"
  }
}

Solution

  • because path depend so c.universe.Tree != x.universe.Tree

    so you need write x type

    implicit val x: c.type = c