blob: d8e86e27c65cb688f8aade119b6cc6100627d0af (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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]
}
}
|