Permalink
Browse files

Test usage of SubpatternsAttachment from a macro

  • Loading branch information...
1 parent 13aa297 commit b9a900e5d2f32b87149f7bd4cc7531f327abf5be @densh densh committed Dec 9, 2013
@@ -0,0 +1,3 @@
+List((a @ Extractor((b @ Extractor((c @ _))))))
+List((b @ Extractor((c @ _))))
+List((c @ _))
@@ -0,0 +1,18 @@
+import scala.reflect.macros.WhiteboxContext
+import language.experimental.macros
+
+object Extractor {
+ def unapply(x: Any): Any = macro unapplyImpl
+ def unapplyImpl(c: WhiteboxContext)(x: c.Tree) = {
+ val st = c.universe.asInstanceOf[reflect.internal.SymbolTable]
+ import st._
+ val subpatterns = x.attachments.get[SubpatternsAttachment].get.patterns
+ q"""
+ new {
+ def isEmpty = false
+ def get = ${subpatterns.toString}
+ def unapply(x: Any) = this
+ }.unapply(${x.asInstanceOf[st.Tree]})
+ """.asInstanceOf[c.Tree]
+ }
+}
@@ -0,0 +1,5 @@
+object Test extends App {
+ 42 match {
+ case Extractor(a @ Extractor(b @ Extractor(c))) => println(a); println(b); println(c)
+ }
+}

0 comments on commit b9a900e

Please sign in to comment.