我想实现一个Scala宏,它接受一个部分函数,对函数的模式执行一些转换,然后将它应用于给定的表达式。
为此,我开始使用以下代码:
def myMatchImpl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: Context)(expr: c.Expr[A])(patterns: c.Expr[PartialFunction[A, B]]): c.Expr[B] = {
import c.universe._
/*
* Deconstruct the partial function and select the relevant case definitions.
*
* A partial, anonymus function will be translated into a new class of the following form:
*
* { @SerialVersionUID(0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[A,B] with Serializable {
*
* def <init>(): anonymous class $anonfun = ...
*
* final override def applyOrElse[...](x1: ..., default: ...): ... = ... match {
* case ... => ...
* case (defaultCase$ @ _) => default.apply(x1)
* }
*
* def isDefined ...
* }
* new $anonfun()
* }: PartialFunction[A,B]
*
*/
val Typed(Block(List(ClassDef(a, b, x, Template(d, e, List(f, DefDef(g, h, i, j, k, Match(l, allCaseDefs)), m)))), n), o) = patterns.tree
/* Perform transformation on all cases */
val transformedCaseDefs: List[CaseDef] = allCaseDefs map {
case caseDef => caseDef // This code will perform the desired transformations, now it's just identity
}
/* Construct anonymus partial function with transformed case patterns */
val result = Typed(Block(List(ClassDef(a, b, x, Template(d, e, List(f, DefDef(g, h, i, j, k, Match(l, transformedCaseDefs)), m)))), n), o)
// println(show(result))
c.Expr[B](q"$result($expr)")
}
我解构部分函数,选择applyOrElse函数的大小写定义,对每个定义执行所需的转换,然后将所有内容重新组合在一起。像这样调用宏:
def myMatch[A, B](expr: A)(patterns: PartialFunction[A, B]): B = macro myMatchImpl[A,B]
不幸的是,这不能按预期工作。在一个简单的例子中使用宏
def test(x: Option[Int]) = myMatch(x){
case Some(n) => n
case None => 0
}
导致以下错误消息:
object creation impossible, since method isDefinedAt in trait PartialFunction of type (x: Option[Int])Boolean is not defined
这有点令人困惑,因为打印生成的部分函数会产生
({
final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[Option[Int],Int] with Serializable {
def <init>(): anonymous class $anonfun = {
$anonfun.super.<init>();
()
};
final override def applyOrElse[A1 <: Option[Int], B1 >: Int](x2: A1, default: A1 => B1): B1 = ((x2.asInstanceOf[Option[Int]]: Option[Int]): Option[Int] @unchecked) match {
case (x: Int)Some[Int]((n @ _)) => n
case scala.None => 0
};
final def isDefinedAt(x2: Option[Int]): Boolean = ((x2.asInstanceOf[Option[Int]]: Option[Int]): Option[Int] @unchecked) match {
case (x: Int)Some[Int]((n @ _)) => true
case scala.None => true
case (defaultCase$ @ _) => false
}
};
new $anonfun()
}: PartialFunction[Option[Int],Int])
明确定义了isDefinedAt方法。
有没有人有想法,这里有什么问题以及如何做到这一点?
答案 0 :(得分:4)
新的反射API提供了一个Transformer
类,专门用于帮助进行这种树转换:
import scala.language.experimental.macros
import scala.reflect.macros.Context
def myMatchImpl[A: c.WeakTypeTag, B: c.WeakTypeTag](c: Context)(
expr: c.Expr[A]
)(
patterns: c.Expr[PartialFunction[A, B]]
): c.Expr[B] = {
import c.universe._
val transformer = new Transformer {
override def transformCaseDefs(trees: List[CaseDef]) = trees.map {
case caseDef => caseDef
}
}
c.Expr[B](q"${transformer.transform(patterns.tree)}($expr)")
}
def myMatch[A, B](expr: A)(patterns: PartialFunction[A, B]): B =
macro myMatchImpl[A,B]
def test(x: Option[Int]) = myMatch(x) {
case Some(n) => n
case None => 0
}
您可能需要一些额外的机制来确保转换仅应用于您希望将其应用于的案例列表,但通常这种方法比手动转换树更有效。
我仍然很好奇为什么你的版本不起作用,如果你有时间,可能值得在这里为另一个问题组合一个简化的例子。