scala self 类型注释中 this 和 self 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4017357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:32:02  来源:igfitidea点击:

Difference between this and self in self-type annotations?

scala

提问by Zach

In various Scala literature I see some self-type annotations using "this" and others using "self":

在各种 Scala 文献中,我看到一些使用“this”和其他使用“self”的自我类型注释:

trait A { this: B => ... }
trait A { self: B => ... }

Is there any real difference between using "this" or "self"? Does it matter at all what name you use? Is this just as valid?

使用“this”或“self”之间有什么真正的区别吗?你用什么名字有关系吗?这同样有效吗?

trait A { foo: B => ... }

回答by Martin Odersky

All three forms are valid, and have the effect that Bis assumed as the type of thisin class A.

所有三种形式都是有效的,并且具有B被假定为thisin class类型的效果A

The first two variants

前两个变体

trait A { self: B => ... }
trait A { foo: B => ... }

introduce self(respectively, foo) as an alias for thisin trait A. This is useful for accessing the thisreference from an inner class. I.e. you could then use selfinstead of A.thiswhen accessing the thisreference of the trait Afrom a class nested within it. Example:

引入self(分别foo)作为thisin trait的别名A。这对于this从内部类访问引用很有用。即,您可以在从嵌套在其中的类中访问特征的引用时使用self而不是。例子:A.thisthisA

class MyFrame extends JFrame { frame =>    
  getContentPane().add( new JButton( "Hide" ) {
    addActionListener( new ActionListener {
      def actionPerformed( e: ActionEvent ) {
        // this.setVisible( false ) --> shadowed by JButton!
        frame.setVisible( false )
      }
    })
  })
}

The third variant,

第三种变体,

trait A { this: B => ... }

does not introduce an alias for this; it just sets the self type.

不会为 引入别名this;它只是设置 self 类型。

回答by Debilski

There is a difference in that thisalways refers to the object defined by the innermost template.

不同之处在于this始终引用由最内层模板定义的对象。

The expression thiscan appear in the statement part of a template or compound type. It stands for the object being defined by the innermost template or compound type enclosing the reference. If this is a compound type, the type of thisis that compound type. If it is a template of a class or object definition with simple name C, the type of this is the same as the type of C.this. (Scala Ref. §6.5)

表达式this可以出现在模板或复合类型的语句部分。它代表由包含引用的最内层模板或复合类型定义的对象。如果这是一个复合类型,则类型this就是那个复合类型。如果是简单名称C的类或对象定义的模板,则 this 的类型与C的类型相同。this. (Scala 参考文献 §6.5)

So, if you call your self-type foo, you could still refer to it as this(unless, of course, you are in an inner template in which case thiswill refer to the object defined by it – and unless you don't give the inner template's self-type the same name) but obviously not the other way round.

所以,如果你调用你的 self-type foo,你仍然可以将它引用为this(当然,除非你在一个内部模板中,在这种情况下this将引用它定义的对象——除非你没有给出内部模板的自类型同名)但显然不是相反。