Quasiquotes的
Scala文档在解释
Lifting时提到了这一点:
One can also combine lifting and unquote splicing:
scala> val ints = List(1,2,3) scala> val f123 = q"f(..$ints)" f123: universe.Tree = f(1,3) scala> val intss = List(List(1,3),List(4,5),List(6)) scala> val f123456 = q"f(...$intss)" f123456: universe.Tree = f(1,3)(4,5)(6)
具体是代码示例中的提升与非引用拼接的实现?
解决方法
两个例子都在两个例子中同时发生.
Unquoting是将Tree替换为另一个Tree的结构(如插值)的过程.在这个例子中,int不是一个Tree,但是存在一个Liftable [List [T]],允许我们将List [T]取消引用到树中,好像它是一棵树(即Liftable告诉了编译器如何将文字List [Int]转换为树,以便它可以被替换).
引用文档:
Unquote splicing is a way to unquote a variable number of elements.
这里,可变数量的元素将是我们想要取消引用的List中的元素.如果我们做q“f($ints)”,那么我们就可以简单地将整数作为f的单个参数.但也许我们想要将重复参数应用于f.为此,我们使用非引号拼接.
q"f(..$ints) // Using `..` means we get f(1,3) instead of f(List(1,3))
再次,文档说它最好,真的:
Dots near unquotee annotate degree of flattening and are also called splicing rank.
..$
expects argument to be anIterable[Tree]
and...$
expectsIterable[Iterable[Tree]]
.
因此,提升允许我们将List [T]取消引用到树f(x)中,好像它是一个Iterable [Tree],而unquote拼接允许我们取消引用List [T]包含的可变数量的元素作为多个参数F.
以下是不同的相关组合:
val listTree = q"scala.collection.immutable.List(1,3)" val treeList = List(q"1",q"2",q"3") val literalList = List(1,3) scala> q"f($listTree)" // plain unquoting from another Tree res6: reflect.runtime.universe.Tree = f(scala.collection.immutable.List(1,3)) scala> q"f($literalList)" // unquoting from lifting res7: reflect.runtime.universe.Tree = f(scala.collection.immutable.List(1,3)) scala> q"f(..$treeList)" // plain unquote splicing res8: reflect.runtime.universe.Tree = f(1,3) scala> q"f(..$literalList)" // unquote splicing and lifting res9: reflect.runtime.universe.Tree = f(1,3)