做自我:T =>这是:T =>定义特征时具有相同的含义吗?
看来我可以使用 self 或 this 来引用混合实例或更确切地说约束混合实例。例如,这些是等价的吗?
scala> trait A { self: List[_] => }
defined trait A
scala> trait B { this: List[_] => }
defined trait B
这只是一种约定,还是使用与 this
不同的东西可以带来一些好处?
It seems I can use self
or this
for referring to the mixed-in instance or rather to constraint the mixed-in instance. For instance, are those equivalent?
scala> trait A { self: List[_] => }
defined trait A
scala> trait B { this: List[_] => }
defined trait B
Is this just a convention, or using something different than this
provide some benefits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您拥有引用封闭实例的成员类型时,使用“this”以外的名称可能会很有用。例如,
,优于。
在某些情况下
Using a name other than "this" can be useful where you have member types which refer to the enclosing instance. For example,
is preferable to,
in some circumstances.
它可以是任何东西:self、this、meep、blah 等。它仅由编译器用来确定要转换到哪个类(在调用其方法时),并且实际上不会显示在字节码中。
命名时要小心,因为本地标识符会覆盖自身类型定义:
It can be anything: self, this, meep, blah, etc. It is used only by the compiler in determining which class to cast to (when calling methods on it) and does not actually show up in the bytecode.
Take care when naming, because local identifiers override the self type definition: