Skip to content

Commit b9a900e

Browse files
committed
Test usage of SubpatternsAttachment from a macro
1 parent 13aa297 commit b9a900e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
List((a @ Extractor((b @ Extractor((c @ _))))))
2+
List((b @ Extractor((c @ _))))
3+
List((c @ _))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.reflect.macros.WhiteboxContext
2+
import language.experimental.macros
3+
4+
object Extractor {
5+
def unapply(x: Any): Any = macro unapplyImpl
6+
def unapplyImpl(c: WhiteboxContext)(x: c.Tree) = {
7+
val st = c.universe.asInstanceOf[reflect.internal.SymbolTable]
8+
import st._
9+
val subpatterns = x.attachments.get[SubpatternsAttachment].get.patterns
10+
q"""
11+
new {
12+
def isEmpty = false
13+
def get = ${subpatterns.toString}
14+
def unapply(x: Any) = this
15+
}.unapply(${x.asInstanceOf[st.Tree]})
16+
""".asInstanceOf[c.Tree]
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test extends App {
2+
42 match {
3+
case Extractor(a @ Extractor(b @ Extractor(c))) => println(a); println(b); println(c)
4+
}
5+
}

0 commit comments

Comments
 (0)