Kim Stebel
Kim Stebel

Reputation: 42037

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.

Upvotes: 6

Views: 823

Answers (1)

Eugene Burmako
Eugene Burmako

Reputation: 13048

Unfortunately it's not possible right now (and won't be possible in 2.10.0-final), but we have something in works that might help in subsequent point releases :)

Upvotes: 2

Related Questions