Scala自类型注释与 'with'混合

标签 scala traits self-type

Possible Duplicate:
What is the difference between scala self-types and trait subclasses?

我无法区分以下两个代码块之间的区别:

    // Trait B is mixed in and creates a dependency on it
    trait A extends C with B { 
       ...
    }

    // Trait B is put in scope and also creates a dependency on it
    trait A extends C {
       self: B =>
    ...
    }

我是从设计角度问的。

谢谢!

最佳答案

当您使用 self 类型时,您会限制 trait 仅当指定的 self 类型被与其混合的其他类型满足时才使用。您不会在定义的 trait 和声明的自类型之间获得继承关系。这意味着特征本身作为孤立的静态类型,本身不能公开替代 self 类型。 (它被比作 C++ 私有(private)继承,但这是一个很弱的类比)。

关于Scala自类型注释与 'with'混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14633231/

相关文章:

scala - 将外部对象的别名类型作为私有(private)范围参数

scala - 将一系列 ZIO 的所有成功和失败集中在一起?

database - 如何使用 Scala 从存储过程中检索多行?

rust - 我怎样才能在原始 crate 中使用另一个 crate 中的特征实现?

scala - 从特征引用构造函数参数

scala自类型: member of type parameter error

scala - `extends` 允许调用特征中的父类(super class)型方法,但不允许自身类型

scala - Cake Pattern 中显式类型的自引用的用处

scala - java.lang.String 不能转换为 scala.Serializable

iOS 13 黑暗模式 : traitCollectionDidChange only called the first time