scala中的合成函数(synthetic function)

今天看到akka http库中有这么一段函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//R <: Rendering 意思是 renderQuery 可以设置任何Rendering类的子类
def renderQuery[R <: Rendering](r: R, query: Query, charset: Charset,
keep: CharPredicate = `strict-query-char-np`): r.type = {
//函数内部定义的函数
def enc(s: String): Unit = encode(r, s, charset, keep, replaceSpaces = true)
@tailrec def append(q: Query): r.type =
q match {
case Query.Empty ⇒ r
case Query.Cons(key, value, tail) ⇒
if (q ne query) r ~~ '&'
enc(key)
if (value ne Query.EmptyValue) r ~~ '='
enc(value)
append(tail)
}
append(query)
}

我查了下ne 就是和eq 类似的,相当于not equal 和equal的关系。这个函数字面意思就是显示
查询参数的,看到里面的q ne query 这一句来了兴趣,查看stackoverflow上是这么写的:

A synthetic field, instead, is a field generated by the compiler to work around
the underlying JVM limitations, especially when dealing with inner anonymous
classes, a concept extraneous to the JVM

参考资料:
What’s the difference between A<:B and +B in Scala?
Scala的eq,ne,equals,==方法与Java异同
Synthetic Function “##” in scala

欢迎关注我的公众号:沉迷Spring
显示 Gitment 评论
0%