summaryrefslogtreecommitdiff
path: root/test/files/run/macro-subpatterns/Macro_1.scala
blob: 2de6b4da9d6467ec7552aec8b357ca85b2f18ebb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import scala.reflect.macros.whitebox.Context
import language.experimental.macros

object Extractor {
  def unapply(x: Any): Any = macro unapplyImpl
  def unapplyImpl(c: Context)(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]
  }
}