编译器向我抛出“缺少参数类型”。在解决了这个问题后,我意识到在链接部分函数时,您需要明确说明类型,否则编译器会抛出上述错误。现在,你们知道在宏的 reify 中链接部分函数时是否有任何问题?我想我不能更明确地了解部分函数类型:
object Implementations{
def missingParamType_impl(c: whitebox.Context)(a: c.Expr[Int]):c.Expr[PartialFunction[Int,String]] = {
import c.universe._
reify {
val spliced = a.splice
val spliced2 = a.splice * 2
((PartialFunction.apply[Int,String]{
case `spliced` ⇒ a.splice.toString
} : PartialFunction[Int,String]).orElse[Int,String]{
case `spliced2` ⇒ a.splice.toString
} : PartialFunction[Int,String]) : PartialFunction[Int,String]
}
}
}
这就是我调用宏实现的方式:
object Macros {
def missingParamType(a: Int):PartialFunction[Int,String] = macro Implementations.missingParamType_impl
}
我也试过这个:
def missingParamType_impl(c: whitebox.Context)(a: c.Expr[Int]):c.Expr[PartialFunction[Int,String]] = {
import c.universe._
reify {
val spliced = a.splice
val spliced2 = a.splice * 2
val pf1: PartialFunction[Int, String] = {
case `spliced` ⇒ a.splice.toString
}
val pf2: PartialFunction[Int, String] = {
case `spliced2` ⇒ a.splice.toString
}
val PF:PartialFunction[Int, String] = pf1.orElse(pf2)
PF
}
}
还是我从根本上误解了 reify 的工作原理?