加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala Quasiquote提升

发布时间:2020-12-16 18:02:12 所属栏目:安全 来源:网络整理
导读: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),Li
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 an Iterable[Tree] and ...$ expects Iterable[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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读