问题描述
一些Scala API的 this 别名是 self ,
Some Scala APIs alias this to self, for example,
trait Function1[-T1, +R] extends AnyRef { self =>
我知道此别名的一般工作原理,但是看不到诸如Function1之类的特征如何从中受益.除了最初提到的,Function1在其定义中的任何地方都没有使用 self ,那么它的目的是什么?
I know how this aliasing works in general, but don't see how traits such as Function1 benefit from it. Function1 does not use self anywhere in its definition except for the initial mention, so what is its purpose here?
以前已经问过这个问题的各种形式,但是答案并不直接适用.答案已经讨论了自我类型和内部类,但是我在这里看不到如何应用.
Variants of this question have been asked previously, but the answers are not directly applicable. Answers have discussed self types and inner classes, but I don't see how that applies here.
推荐答案
请参见它说的是
// GENERATED CODE: DO NOT EDIT. See scala.Function0 for timestamp.
代码是由同一生成器为 Function0
到 Function22
生成的.以某种方式转到 Function5
时,您开始看到被使用:
The code is generated by the same generator for Function0
through Function22
. Somehow when it goes to Function5
you start seeing self
being used:
self.apply(x1, x2, x3, x4, x5)).curried
因此,我怀疑将 self =>
始终包含在生成器模板中会更容易.
So I suspect it was easier to have self =>
always included in the generator template.
这是提交,其中添加了自引用.提交消息实际上说明了为什么它对n> = 5做一些不同的事情,我引用:
Here is the commit that adds the self reference. The commit message actually explains why it does something different for n >= 5, I quote:
这篇关于使用self =>在scala中对此别名进行别名.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!