Scala 准引用 - Quasiquote介绍
Quasiquotes are a neat notation that lets you manipulate Scala syntax trees with ease:
scala> val tree = q"i am { a quasiquote }"
tree: universe.Tree = i.am(a.quasiquote)
Every time you wrap a snippet of code in q"..." it will become a tree that represents a given snippet. As you might have already noticed, quotation syntax is just another usage of extensible string interpolation, introduced in 2.10. Although they look like strings they operate on syntactic trees under the hood.
The same syntax can be used to match trees as patterns:
scala> println(tree match { case q"i am { a quasiquote }" => "it worked!" })
it worked!
Whenever you match a tree with a quasiquote it will match whenever the structureof a given tree is equivalent to the one you've provided as a pattern. You can check for structural equality manually with the help of equalsStructure method:
scala> println(q"foo + bar" equalsStructure q"foo.+(bar)")
true
You can also put things into quasiquotation with the help of $:
scala> val aquasiquote = q"a quasiquote"
aquasiquote: universe.Select = a.quasiquote
scala> val tree = q"i am { $aquasiquote }"
tree: universe.Tree = i.am(a.quasiquote)
This operation is also known as unquoting. Whenever you unquote an expression of type Tree in a quasiquote it will structurally substitute that tree into that location. Most of the time such substitutions between quotes is equivalent to a textual substitution of the source code.
Similarly, one can structurally deconstruct a tree using unquoting in pattern matching:
scala> val q"i am $what" = q"i am { a quasiquote }"
what: universe.Tree = a.quasiquote
Interpolators
Scala is a language with rich syntax that differs greatly depending on the syntactical context:
scala> val x = q"""
val x: List[Int] = List(1, 2) match {
case List(a, b) => List(a + b)
}
"""
x: universe.ValDef =
val x: List[Int] = List(1, 2) match {
case List((a @ _), (b @ _)) => List(a.$plus(b))
}
In this example we see three primary contexts being used:
List(1, 2)andList(a + b)are expressionsList[Int]is a typeList(a, b)is a pattern
Each of these contexts is covered by a separate interpolator:
| Used for | |
|---|---|
| q | expressions, definitions and imports |
| tq | types |
| pq | patterns |
Syntactical similarity between different contexts doesn't imply similarity between underlying trees:
scala> println(q"List[Int]" equalsStructure tq"List[Int]")
false
If we peek under the hood we’ll see that trees are, indeed different:
scala> println(showRaw(q"List[Int]"))
TypeApply(Ident(TermName("List")), List(Ident(TypeName("Int"))))
scala> println(showRaw(tq"List[Int]"))
AppliedTypeTree(Ident(TypeName("List")), List(Ident(TypeName("Int"))))
Similarly, patterns and expressions are also not equivalent:
scala> println(pq"List(a, b)" equalsStructure q"List(a, b)")
false
It’s extremely important to use the right interpolator for the job in order to construct a valid syntax tree.
Additionally there are two auxiliary interpolators that let you work with minor areas of scala syntax:
| Used for | |
|---|---|
| cq | case clause |
| fq | for loop enumerator |
See the section syntax summary for details.
Splicing
Unquote splicing is a way to unquote a variable number of elements:
scala> val ab = List(q"a", q"b")
scala> val fab = q"f(..$ab)"
fab: universe.Tree = f(a, b)
Dots before the unquotee annotate indicate a degree of flattenning and are called a splicing rank. ..$ expects the argument to be an Iterable[Tree] and ...$ expects an Iterable[Iterable[Tree]].
Splicing can easily be combined with regular unquotation:
scala> val c = q"c"
scala> val fabc = q"f(..$ab, $c)"
fabc: universe.Tree = f(a, b, c)
scala> val fcab = q"f($c, ..$ab)"
fcab: universe.Tree = f(c, a, b)
scala> val fabcab = q"f(..$ab, $c, ..$ab)"
fabcab: universe.Tree = f(a, b, c, a, b)
If you want to abstract over applications even further, you can use ...$:
scala> val argss = List(ab, List(c))
arglists: List[List[universe.Ident]] = List(List(a, b), List(c))
scala> val fargss = q"f(...$argss)"
fargss: universe.Tree = f(a, b)(c)
At the moment ...$ splicing is only supported for function applications and parameter lists in def and class definitions.
Similarly to construction one can also use ..$ and ...$ to tear trees apart:
scala> val q"f(..$args)" = q"f(a, b)"
args: List[universe.Tree] = List(a, b)
scala> val q"f(...$argss)" = q"f(a, b)(c)"
argss: List[List[universe.Tree]] = List(List(a, b), List(c))
There are some limitations in the way you can combine splicing with regular $variable extraction:
case q"f($first, ..$rest)" => // ok
case q"f(..$init, $last)" => // ok
case q"f(..$a, ..$b)" => // not allowed
So, in general, only one ..$ is allowed per given list. Similar restrictions also apply to ...$:
case q"f(..$first)(...$rest)" => // ok
case q"f(...$init)(..$first)" => // ok
case q"f(...$a)(...$b)" => // not allowed
In this section we only worked with function arguments but the same splicing rules are true for all syntax forms with a variable number of elements. Syntax summaryand the corresponding details sections demonstrate how you can use splicing with other syntactic forms.
Scala 准引用 - Quasiquote介绍的更多相关文章
- Scala进阶之路-Scala中的泛型介绍
Scala进阶之路-Scala中的泛型介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 通俗的讲,比如需要定义一个函数,函数的参数可以接受任意类型.我们不可能一一列举所有的参数类 ...
- 基于OGG的Oracle与Hadoop集群准实时同步介绍
版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/220 来源:腾云阁 https://www.qclou ...
- Python学习四|变量、对象、引用的介绍
变量 变量创建:一个变量也就是变量名,就像a,当代码第一次赋值时就创建了它.之后的赋值将会改变已创建的变量名的值,从技术上讲,Python在代码运行之前先检测变量名,可以当成是最初的赋值创建了变量. ...
- 01_Java 软、弱引用语法介绍
文章导读: 从JDK1.2版本开始,把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期.这四种级别由高到低依次为:强引用.软引用.弱引用和虚引用, 本章内容介绍了Reference的概 ...
- 对C++11中的`移动语义`与`右值引用`的介绍与讨论
本文主要介绍了C++11中的移动语义与右值引用, 并且对其中的一些坑做了深入的讨论. 在正式介绍这部分内容之前, 我们先介绍一下rule of three/five原则, 与copy-and-swap ...
- jdb调试scala代码的简单介绍
在linux调试C/C++的代码需要通过gdb,调试java代码呢?那就需要用到jdb工具了.关于jdb的用法在网上大家都可以找到相应的文章,但是对scala进行调试的就比较少了.其实调试的大致流程都 ...
- 第4节 Scala中的actor介绍:1、actor概念介绍;2、actor执行顺序和发送消息的方式
10. Scala Actor并发编程 10.1. 课程目标 10.1.1. 目标一:熟悉Scala Actor并发编程 10.1.2. 目标二:为学习Akka做准备 注:Sca ...
- 神奇的Scala Macro之旅(三)- 实际应用
在上一篇中,我们示范了使用macro来重写 Log 的 debug/info 方法,并大致的介绍了 macro 的基本语法.基本使用方法.以及macro背后的一些概念, 如AST等.那么,本篇中,我们 ...
- Java 四种引用介绍及使用场景
强引用-FinalReference 介绍: 强引用是平常中使用最多的引用,强引用在程序内存不足(OOM)的时候也不会被回收,使用方式: String str = new String("s ...
随机推荐
- 初探云原生应用管理之:聊聊 Tekton 项目
[编者的话]“人间四月芳菲尽,山寺桃花始盛开.” 越来越多专门给 Kubernetes 做应用发布的工具开始缤纷呈现,帮助大家管理和发布不断增多的 Kubernetes 应用.在做技术选型的时候,我们 ...
- mongodb 更新数据时int32变为double的解决办法 & 教程
https://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html mongodb 更新数据时int32变为double的解决办法 ...
- Winform 美化
首先,我们先来实现主界面的扁平化 此处分为两个步骤,第一步是更改winform自带的MainForm窗体属性,第二步是添加窗体事件. 将主窗体FormBorderStyle更改为None,这样就得到了 ...
- 教你使用 Swoole-Tracker 秒级定位 PHP 卡死问题
PHPer 肯定收到过这样的投诉:小菊花一直在转!你们网站怎么这么卡!当我们线上业务遇到这种卡住(阻塞)的情况,大部分 PHPer 会两眼一抹黑,随后想起那句名言:性能瓶颈都在数据库然后把锅甩给DBA ...
- JavaScript RegExp(正则表达式) 对象
正则表达式是描述字符模式的对象.正则表达式用于在文本上执行模式匹配和“搜索和替换”功能. var patt = /JC2182/i 示例说明: /JC2182/i - 是一个正则表达式. JC2182 ...
- html 实体编码转换成原字符
今天遇到件很恶心的事,某国外歌词网站提供的歌词在源文件里使用“&#数字;”格式的编码表示abcd....原来小菜我实在才疏学浅不知此为何物,于是特有的搜索引擎控,搜之.片刻得解,此乃html实 ...
- SAP MM 同一个序列号可以被多次用在交货单发货过账?
SAP MM 同一个序列号可以被多次用在交货单发货过账? 如下公司间转储订单,从公司代码CSAS转入公司代码HKCS, 物料有启用序列号管理. 转储数量为5 PC.该STO单据共计有2个外向交货单 8 ...
- 苹果审核之遇到IPV6问题被拒的解决方法
情景: 等待苹果审核上线时,发现因为IPV6被拒了.这是悲剧,以下是苹果审核给我的理由: We discovered one or more bugs on Wi-Fi connected to an ...
- 详解Vue响应式原理
摘要: 搞懂Vue响应式原理! 作者:浪里行舟 原文:深入浅出Vue响应式原理 Fundebug经授权转载,版权归原作者所有. 前言 Vue 最独特的特性之一,是其非侵入性的响应式系统.数据模型仅仅是 ...
- SparkStreaming实战(数据库(NoSQL))
完全搞清楚项目需求,思考项目选项,这块就是使用的是数据库,就是HBase,因为它里面有一个非常合适的API,直接调用,即可 功能一: 今天到现在为止 实战课程 的访问量 yyyyMMdd 使用数据库来 ...