Scala:'缺少参数类型'使用PartialFunction reify调用scala宏时

时间:2015-02-25 11:04:05

标签: scala macros partialfunction

编译器给我一个' 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
    }
  }

或者我是否从根本上误解了如何运作?

1 个答案:

答案 0 :(得分:1)

不幸的是,这看起来像一个已知问题:https://issues.scala-lang.org/browse/SI-6619