gpt4 book ai didi

scala - self => 是用来做什么的?

转载 作者:行者123 更新时间:2023-12-03 04:44:32 25 4
gpt4 key购买 nike

Possible Duplicate:
What does “outer =>” really mean?

当我查看以下源代码时:scala/src/library/scala/Option.scala

sealed abstract class Option[+A] extends Product with Serializable {
self =>

我想知道 self 有什么用。我知道 self 类型的正常用途是限制可以混合特征的类。例如:

scala> trait A
defined trait A

scala> trait NeedA {self: A =>}
defined trait NeedA

scala> new NeedA {}
<console>:10: error: illegal inheritance;
self-type java.lang.Object with NeedA does not conform to NeedA's selftype NeedA with A
new NeedA {}
^

scala> new NeedA with A {}
res39: java.lang.Object with NeedA with A = $anon$1@4d04a0e8

scala>

但“this =>”并非如此。这个“this =>”到底是用来做什么的?

最佳答案

它创建了一个别名,这在内部类中可能很方便(与 OuterClass.this 同义)

class A {self => 
...
class B {
// self is the enclosing A, synonymous for A.this
}
}

关于scala - self => 是用来做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8533201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com