`
zhouchaofei2010
  • 浏览: 1085897 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

scala自身类型 self-type 的作用

阅读更多

self type 作用通常为了分割大类到多个特质traits的。比如cake pattern蛋糕模式中会用到self type

官网解释如下:
 
 

self type

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小节给出的例子代码如下

 

 

 

29.4 Splitting modules into traits
 
package quickanswer.chapter10
 
class 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 SimpleRecipes
 
trait 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 解决了这个问题

 
从技术上讲,self type 是一个表示this类型的假定的类型。从编程角度来说,self type 要求具体的类必须混入指定的特质,否则会报编译错误。 如过有一个trait 是混入在其他trait中的,则其他trait需要用假定的类型self type
 

当SimpleRecipes 添加 this: SimpleFoods =>   后,Pear会被暗中转化为this.Pear ,这样是安全的,因为任何混入SimpleRecipes 的具体类必须也要混入SimpleFoods  。这意味着Pear会是这个具体类的一个成员。混入SimpleRecipes 的抽象类和trait没哟这个限制,但是他们是不能通过new 实例化的。所哟this.Pear 是安全的

 
 
 

书本解释如下"

 
The problem here is that Pear is located in a different trait from the one that
uses it, so it is out of scope. The compiler has no idea that SimpleRecipes
is only ever mixed together with SimpleFoods.
There is a way you can tell this to the compiler, however. Scala provides
the self type for precisely this situation. Technically, a self type is an assumed
type for this whenever this is mentioned within the class. Pragmatically,
a self type specifies the requirements on any concrete class the trait is mixed
into. If you have a trait that is only ever used when mixed in with another
trait or traits, then you can specify that those other traits should be assumed.
In the present case, it is enough to specify a self type of SimpleFoods, as
shown in Listing 29.15:
 
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)
}
Listing 29.15 · A SimpleRecipes trait with a self type.
 
Given the new self type, Pear is now available. Implicitly, the reference
to Pear is thought of as this.Pear. This is safe, because any concrete
class that mixes in SimpleRecipes must also be a subtype of SimpleFoods,
which means that Pear will be a member. Abstract subclasses and traits do
not have to follow this restriction, but since they cannot be instantiated with
new, there is no risk that the this.Pear reference will fail
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics