简体   繁体   English

宏,拼接和模式匹配

[英]macros, splice, and pattern matching

Is there a way to use an argument to a macro in a pattern match? 有没有办法在模式匹配中对宏使用参数? I would like to do this: 我想这样做:

def extr(X:AnyRef) = macro extrImpl

def extrImpl(c:Context)(X:c.Expr[AnyRef]):c.Expr[AnyRef] = {
  import c.universe._

  val tree = reify {
    new {
      def unapply(x:String):Option[String] = x match {
        case X.splice => Some(x) //error
        case _ => None
      }
    }
  }.tree
  c.Expr(c.typeCheck(tree))
}

But unfortunately the compiler says "stable identifier required, but X.splice found". 但是不幸的是,编译器说“需要稳定的标识符,但是找到了X.splice”。 Normally, one would solve this by assigning to a val first, such as: 通常,可以通过先分配一个val来解决此问题,例如:

val XX = X.splice

But of course that doesn't work with splice either. 但是,当然这对拼接也不起作用。

不幸的是,目前尚不可能(并且在2.10.0-final中将无法实现),但是我们正在进行一些工作,可能会在以后的版本发布中有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM