Code used for the the following examples.
val tree =
q"""
val a = 5
val c = 3
c = 5
if (3 == 17) {
val c = 1
}
else 2
5
"""
Definition
def collect[C[_]] = new {
def apply[A](f: PartialFunction[T, A]): C[A] //or List[A] if C[_] is infered to Nothing
}
Example
val l1 = tree.collect[Set]{case Dec.Val(_,a,_,_) => a.head.toString}
l1: Set[String] = Set(a, c)
val l2 = tree.collect{case Lit.Int(a) => a}
l2: List[Int] = List(5, 3, 5, 3, 17, 1, 2, 5)