如何判断一个对象是宏中 Traversable(List, Seq..) 的伴随对象
How to tell an object is companion object of a Traversable(List, Seq..) in macro
case q"$pack.$coll.apply[..$t](..$v)" if isTraverable(coll) => xxx
我正在尝试匹配 Array.apply
、List.apply
、Seq.apply
、Set.apply
和其他 Traverable
应用方法。
我应该如何实施isTraverable(coll)
object isTraverable {
def apply[V](v: V): Any = macro impl[V]
def impl[V: c.WeakTypeTag](c: whitebox.Context)(v: c.Expr[V]) = {
import c.universe._
def isTraverable(coll: Tree) = {
coll.tpe.typeConstructor <:< typeOf[TraversableOnce[_]].typeConstructor
}
def pattern(tree: Tree): Unit = tree match {
// NODE : coll.tye is not typeOf[List[_]] but rather typeOf[List.type]
case q"$coll.apply[..$t](..$agrs)" if isTraverable(tree) =>
println(s"${coll} <:< TraversableOnce")
pattern(agrs.last)
case other =>
println(s"$other is not TraversableOnce")
}
//immutable.this.List <:< TraversableOnce
//immutable.this.List <:< TraversableOnce
//immutable.this.List <:< TraversableOnce
//3 is not TraversableOnce
pattern(v.tree)
q"()"
}
}
测试
isTraverable.apply(
List(
List(
List(1, 2, 3)
)
))
case q"$pack.$coll.apply[..$t](..$v)" if isTraverable(coll) => xxx
我正在尝试匹配 Array.apply
、List.apply
、Seq.apply
、Set.apply
和其他 Traverable
应用方法。
我应该如何实施isTraverable(coll)
object isTraverable {
def apply[V](v: V): Any = macro impl[V]
def impl[V: c.WeakTypeTag](c: whitebox.Context)(v: c.Expr[V]) = {
import c.universe._
def isTraverable(coll: Tree) = {
coll.tpe.typeConstructor <:< typeOf[TraversableOnce[_]].typeConstructor
}
def pattern(tree: Tree): Unit = tree match {
// NODE : coll.tye is not typeOf[List[_]] but rather typeOf[List.type]
case q"$coll.apply[..$t](..$agrs)" if isTraverable(tree) =>
println(s"${coll} <:< TraversableOnce")
pattern(agrs.last)
case other =>
println(s"$other is not TraversableOnce")
}
//immutable.this.List <:< TraversableOnce
//immutable.this.List <:< TraversableOnce
//immutable.this.List <:< TraversableOnce
//3 is not TraversableOnce
pattern(v.tree)
q"()"
}
}
测试
isTraverable.apply(
List(
List(
List(1, 2, 3)
)
))