What is the difference between self-types and trait subclasses?

后端 未结 11 2095
名媛妹妹
名媛妹妹 2020-11-22 08:53

A self-type for a trait A:

trait B
trait A { this: B => }

says that \"A cannot be mixed into a concrete cl

11条回答
  •  無奈伤痛
    2020-11-22 09:21

    TL;DR summary of the other answers:

    • Types you extend are exposed to inherited types, but self-types are not

      eg: class Cow { this: FourStomachs } allows you to use methods only available to ruminants, such as digestGrass. Traits that extend Cow however will have no such privileges. On the other hand, class Cow extends FourStomachs will expose digestGrass to anyone who extends Cow .

    • self-types allow cyclical dependencies, extending other types does not

提交回复
热议问题