自我类型和特征子类之间有什么区别? [英] What is the difference between self-types and trait subclasses?

查看:144
本文介绍了自我类型和特征子类之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特质A的自类型:

trait B
trait A { this: B => }

"A不能混入不扩展B" 的具体类中.

says that "A cannot be mixed into a concrete class that does not also extend B".

另一方面,以下内容:

trait B
trait A extends B

A中混合的任何(具体或抽象的)类也将在B中混合".

says that "any (concrete or abstract) class mixing in A will also be mixing in B".

这两个语句不是同一回事吗?自类型似乎仅用于创建简单的编译时错误.

Don't these two statements mean the same thing? The self-type seems to serve only to create the possibility of a simple compile-time error.

我想念什么?

推荐答案

主要用于依赖注入,例如在Cake Pattern中.有一篇出色的文章涵盖了Scala中许多不同形式的依赖注入,包括蛋糕图案.如果您使用Google"Cake Pattern and Scala",则会获得许多链接,包括演示文稿和视频.目前,这是指向另一个问题的链接.

It is predominately used for Dependency Injection, such as in the Cake Pattern. There exists a great article covering many different forms of dependency injection in Scala, including the Cake Pattern. If you Google "Cake Pattern and Scala", you'll get many links, including presentations and videos. For now, here is a link to another question.

现在,关于自我类型和扩展特质之间的区别是很简单的.如果您说B extends A,则B 一个A.使用自类型时,B 需要一个A.使用自类型创建的两个特定要求是:

Now, as to what is the difference between a self type and extending a trait, that is simple. If you say B extends A, then B is an A. When you use self-types, B requires an A. There are two specific requirements that are created with self-types:

  1. 如果扩展了B,则需要 混入A.
  2. 当具体类最终扩展/混合了这些特征时,某些类/特征必须实现A.
  1. If B is extended, then you're required to mix-in an A.
  2. When a concrete class finally extends/mixes-in these traits, some class/trait must implement A.

请考虑以下示例:

scala> trait User { def name: String }
defined trait User

scala> trait Tweeter {
     |   user: User =>
     |   def tweet(msg: String) = println(s"$name: $msg")
     | }
defined trait Tweeter

scala> trait Wrong extends Tweeter {
     |   def noCanDo = name
     | }
<console>:9: error: illegal inheritance;
 self-type Wrong does not conform to Tweeter's selftype Tweeter with User
       trait Wrong extends Tweeter {
                           ^
<console>:10: error: not found: value name
         def noCanDo = name
                       ^

如果TweeterUser的子类,则不会出现错误.在上面的代码中,每当使用Tweeter时,我们需要一个User,但是没有为Wrong提供User,因此出现错误.现在,在上面的代码仍在范围内的情况下,请考虑:

If Tweeter was a subclass of User, there would be no error. In the code above, we required a User whenever Tweeter is used, however a User wasn't provided to Wrong, so we got an error. Now, with the code above still in scope, consider:

scala> trait DummyUser extends User {
     |   override def name: String = "foo"
     | }
defined trait DummyUser

scala> trait Right extends Tweeter with User {
     |   val canDo = name
     | }
defined trait Right 

scala> trait RightAgain extends Tweeter with DummyUser {
     |   val canDo = name
     | }
defined trait RightAgain

对于Right,满足混入User的要求.但是,上面提到的第二个要求没有得到满足:对于扩展Right的类/特征,实现User的负担仍然存在.

With Right, the requirement to mix-in a User is satisfied. However, the second requirement mentioned above is not satisfied: the burden of implementing User still remains for classes/traits which extend Right.

使用RightAgain可以同时满足这两个要求.提供了UserUser的实现.

With RightAgain both requirements are satisfied. A User and an implementation of User are provided.

有关更实际的用例,请参阅此答案开头的链接!但是,希望现在您能理解.

For more practical use cases, please see the links at the start of this answer! But, hopefully now you get it.

这篇关于自我类型和特征子类之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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