scala - 在 Scala 中将函数文字与 quasiquotes 匹配

标签 scala macros scala-macros scala-quasiquotes

这个问题的动机与我的 previous question 相似(尽管这是关于我在 different context 中遇到的问题)。

我可以很容易地对函数文字进行模式匹配,而无需 quasiquotes:

import scala.reflect.macros.Context
import scala.language.experimental.macros

object QQExample {
  def funcDemo(f: Int => String) = macro funcDemo_impl
  def funcDemo_impl(c: Context)(f: c.Expr[Int => String]) = {
    import c.universe._

    f.tree match {
      case Function(ps, body) => List(ps, body) foreach println
      case _ => c.abort(
        c.enclosingPosition,
        "Must provide a function literal."
      )
    }

    c.literalUnit
  }
}

它的工作原理是这样的:
scala> QQExample.funcDemo((a: Int) => a.toString)
List(val a: Int = _)
a.toString()

现在假设我想使用 quasiquotes 更灵活地进行相同类型的匹配。以下内容也将匹配该功能,并打印出我们期望的内容。
case q"($x: $t) => $body" => List(x, t, body) foreach println

但是如果我想在模式中指定类型,它不匹配:
case q"($x: Int) => $body" => List(x, body) foreach println

并且以下任何内容都无法编译:
case q"$p => $body"      => List(p,  body) foreach println
case q"($p) => $body"    => List(p,  body) foreach println
case q"..$ps => $body"   => List(ps, body) foreach println
case q"(..$ps) => $body" => List(ps, body) foreach println

是否可以在使用 quasiquotes 匹配函数文字时指定参数的类型,或者匹配未知数量的参数?

最佳答案

使用 2.10 和 vanilla 2.11 的最新天堂插件,您可以这样做:

val q"(..$args) => $body" = f.tree

我刚刚用 paradise example project 测试过了与以下 Macros.scala :
import language.experimental.macros
import scala.reflect.macros.Context

object Macro {
  def apply(f: Any): Any = macro impl
  def impl(c: Context)(f: c.Expr[Any]) = { import c.universe._
    val q"(..$args) => $body" = f.tree
    println(s"args = $args, body = $body")
    c.Expr(q"()")
  }
}

Test.scala :
object Test extends App {
  Macro((x: Int) => x + 1)
}

您可以在 corresponding chapter of quasiquote guide 中阅读有关使用准引号处理函数树的更多信息。 .

关于scala - 在 Scala 中将函数文字与 quasiquotes 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560023/

相关文章:

macros - 药剂宏: Power ** function

scala - 在 Scala 2.10 中使用带有宏的附件

scala - 如何指定多个类的宏注解扩展顺序?

scala - 如何获得 Scala 2.10 反射引用的实际对象?

java - Scala 2.9.2 无法处理 String 类的重载格式方法 - 为什么?

scala - 为什么 lines.map 不起作用,但 lines.take.map 在 Spark 中起作用?

c++ - 在 C 和 C++ 中,我可以使用包含空格的宏定义吗?

Scala 类型边界

scala - 是否可以从 Tree 和 MethodSymbol 生成 Apply?

scala 3 内联失败一个非常简单的例子