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

后端 未结 11 2192
名媛妹妹
名媛妹妹 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:22

    One additional difference is that self-types can specify non-class types. For instance

    trait Foo{
       this: { def close:Unit} => 
       ...
    }
    

    The self type here is a structural type. The effect is to say that anything that mixes in Foo must implement a no-arg "close" method returning unit. This allows for safe mixins for duck-typing.

提交回复
热议问题