2008/09/16

Scala, Object as Module

Package provides no way to abstract?

  • Package is not configurable
  • Package is not inheritable
Self type: (scalabook) self type is the assumed type for this.
If a formal parameter is given, it can be used as an alias for the reference this throughout the body of the template. If the formal parameter comes with a type T , this definition affects the self type S of the underlying class or object as follows: Let C be the type of the class or trait or object defining the template. If a type T is given for the formal self parameter, S is the greatest lower bound of T and C. If no type T is given, S is just C. Inside the template, the type of this is assumed to be S.
举个简单的例子, 对于一个trait A而言,无法预期会被哪一个class或是object继承(mixin),因此在这个trait中,this默认为类型A,在这个trait中只能访问这个trait或是这个trait的父类型的元素。然而

trait A{
self: B=>
}

引入了如下变化:
  • 只有类型B或B的子类型class或object可以继承A(mixin),否则编译错误;
  • trait A内部还可以使用类型B所定义的元素;

0 comments:

Footer

  © Blogger template 'Grease' by Ourblogtemplates.com 2008

Back to TOP