self type 作用通常为了分割大类到多个特质traits的。比如cake pattern蛋糕模式中会用到self type
self type
A self type of a trait is the assumed type of this
, the receiver, to be used within the trait. Any concrete class that mixes in the trait must ensure that its type conforms to the trait’s self type. The most common use of self types is for dividing a large class into several traits (as described in Chapter 29 of Programming in Scala.
例子在<<Programming in Scala>> 第29章 第4小节给出的例子代码如下
package quickanswer.chapter10class Food(fn:String){var name:String=fn}class Recipe(var rn:String,var foodLst:List[Food],var desc:String){//食谱}trait FoodCategories {case class FoodCategory(name: String, foods: List[Food])def allCategories: List[FoodCategory]}trait SimpleFoods {object Pear extends Food("Pear")object Apple extends Food("Apple")def allFoods = List(Apple, Pear)def allCategories = Nil}abstract class Database extends FoodCategories {def allFoods: List[Food]def allRecipes: List[Recipe]def foodNamed(name: String) =allFoods.find(f => f.name == name)}object SimpleDatabase extends Database with SimpleFoods with SimpleRecipestrait SimpleRecipes { // Does not compile// this: SimpleFoods => // 注释掉这句话会编译错误,因为 Apple, Pear 是定义在SimpleFoods 的object FruitSalad extends Recipe("fruit salad",List(Apple, Pear), // Uh oh"Mix it all together.")def allRecipes = List(FruitSalad)}object SplittingModulesIntoTraits extends App {}
dividing a large class( SimpleDatabase ) into several traits (SimpleFoods ,SimpleRecipes),而在 定义trait SimpleRecipes 中又需要SimpleFoods 中的内容, 但是编译器器并不知道 SimpleRecipes 是混入SimpleFoods 的,而 Apple, Pear 是定义在SimpleFoods中的的,所以编译会报错 , 而通过self type 解决了这个问题
当SimpleRecipes 添加 this: SimpleFoods => 后,Pear会被暗中转化为this.Pear ,这样是安全的,因为任何混入SimpleRecipes 的具体类必须也要混入SimpleFoods 。这意味着Pear会是这个具体类的一个成员。混入SimpleRecipes 的抽象类和trait没哟这个限制,但是他们是不能通过new 实例化的。所哟this.Pear 是安全的
书本解释如下"
trait SimpleRecipes {this: SimpleFoods =>object FruitSalad extends Recipe("fruit salad",List(Apple, Pear), // Now Pear is in scope"Mix it all together.")def allRecipes = List(FruitSalad)}