Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 762
Kim Stebel Avatar asked Sep 28 '12 10:09

Kim Stebel


1 Answers

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 :)

like image 119
Eugene Burmako Avatar answered Nov 15 '22 06:11

Eugene Burmako