编译器给我一个' Missing参数类型'。转弯问题之后,我意识到链接部分函数时需要明确表示类型,否则编译器会抛出上述错误。现在,你们知道在宏的内部链接部分函数时是否存在任何问题?我想我对部分函数类型不太明确:
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
}
}
或者我是否从根本上误解了如何运作?