简体   繁体   中英

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". Normally, one would solve this by assigning to a val first, such as:

val XX = X.splice

But of course that doesn't work with splice either.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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