【问题标题】:Calling function with implicit parameter from quasioquote使用来自 quasioquote 的隐式参数调用函数
【发布时间】:2016-01-21 11:58:56
【问题描述】:

由于某种原因,每次我尝试使用准引号中的隐式参数调用函数时,它都会失败 Can't unquote x.universe.Tree, consider providing an implicit instance of Liftable[x.universe.Tree]

这有什么问题?我不允许这样做吗?我找不到任何地方说我不能这样做

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()"
  }
}

【问题讨论】:

    标签: scala implicit scala-macros scala-quasiquotes scala-macro-paradise


    【解决方案1】:

    因为路径依赖所以c.universe.Tree != x.universe.Tree

    所以你需要写 x 类型

    implicit val x: c.type = c
    

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2017-04-16
      • 2018-08-15
      相关资源
      最近更新 更多