使用self =>在scala中对此别名进行别名. [英] Aliasing this in scala with self =>

查看:60
本文介绍了使用self =>在scala中对此别名进行别名.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些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.

推荐答案

请参见代码是由同一生成器为 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:

FunctionN,其中N> 4,在动态创建更多对象的开销(这似乎很合理)鉴于此类功能可能有多常见).这也允许在FunctionN中使用N> 8咖喱,而不会遇到文件名长度限制.

这篇关于使用self =>在scala中对此别名进行别名.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆