在scala中的父特征中的一个子特征的类型
我有一些相同的自我类型的斯卡拉特征声明如下。
trait BookDbModule {
  self: DbConfig => // Abstract this to a parent trait
  /* ... */
}
trait AuthorDbModule {
  self: DbConfig => // Abstract this to a parent trait
  /* ... */
}
我试图将自我类型声明抽象为父类特征,使得每个特征不必定义自我类型。 我尝试了以下。
trait DbModule {
  self: DbConfig =>
  // Some common DbModule methods
}
// !!! Illegal Inheritance, self-type BookDbModule does not conform to DbConfig
trait BookDbModule extends DbModule {
  // What needs to be used instead of extends?
  /* ... */
}
// !!! Illegal Inheritance, self-type AuthorDbModule does not conform to DbConfig
trait AuthorDbModule extends DbModule {
  // What needs to be used instead of extends?
  /* ... */
}
  由于BookDbModule不会扩展DbConfig , BookDbModule错误消息Illegal Inheritance对我来说是DbConfig 。 
斯卡拉有什么方法来强化父母特质中的自我类型的儿童特质?
更新:似乎这个问题有点混乱。
  我想要实现的是,我想通过扩展(或任何其他scala特性)父特征DbModule同时具有自我类型DbConfig ,省略为BookDbModule和AuthorDbModule设置自我类型的DbConfig 。 
  所以,基本上,我正在寻找一种方法,通过在父DbModule声明自我类型而不是在那些子特征中, DbConfig通过具有DbConfig的类来扩展儿童特征( BookDbModule和AuthorDbModule )。 
// This works but is there any way to omit necessity to write
// self: DbConfig =>
trait AuthorDbModule extends DbModule {
  self: DbConfig =>
  /* ... */
}
请让我知道它是否仍然令人困惑。
谢谢!
看看这个:
scala> trait DbConfig { def f = 123 }
defined trait DbConfig
  DbModule需要DbConfig实现: 
scala> trait DbModule { self: DbConfig => }
defined trait DbModule
  BookDbModule的类型是DbModule ,仍然需要DbConfig实现: 
scala> trait BookDbModule extends DbModule { self: DbConfig => }
defined trait BookDbModule
scala> new BookDbModule with DbConfig {}.f
res0: Int = 123
  BookDbModule的类型是BookDbModule ,需要DbConfig直接执行: 
scala> trait BookDbModule { self: DbConfig => }
defined trait BookDbModule
scala> new BookDbModule with DbConfig {}.f
res1: Int = 123
  BookDbModule的类型是BookDbModule ,需要DbModule实施,这又需要DbConfig实现: 
scala> trait BookDbModule { self: DbModule => }
defined trait BookDbModule
scala> new BookDbModule with DbConfig {}.f
<console>:14: error: illegal inheritance;
 self-type BookDbModule with DbConfig does not conform to BookDbModule's selftype BookDbModule with DbModule
       new BookDbModule with DbConfig {}.f
           ^
scala> new BookDbModule with DbConfig with DbModule {}.f
res3: Int = 123
你也可以使用继承:
trait BookDbModule extends DbModule with DbConfig
scala> new BookDbModule with DbConfig {}.f
res4: Int = 123
然而,你不能以某种方式继承一个自我类型注释,所以你可以使用继承,或者明确地用自我类型注释。 请注意,这种简化也是可能的:
scala> trait DbConfig { def f = 123 }
defined trait DbConfig
scala> trait DbModule { self: DbConfig => }
defined trait DbModule
scala> trait DbModuleService extends DbModule with DbConfig
defined trait DbModuleService
  最接近您要查找的内容,但必须使用“完整”的中间特质DbModuleService : 
scala> trait BookDbModule extends DbModuleService
defined trait BookDbModule
scala> new BookDbModule {}.f
res0: Int = 123
要么:
scala> trait DbConfig { def f = 123 }
defined trait DbConfig
scala> trait DbModule { self: DbConfig => }
defined trait DbModule
scala> trait DbModuleService extends DbModule with DbConfig
defined trait DbModuleService
scala> trait BookDbModule { self: DbModuleService => }
defined trait BookDbModule
scala> new BookDbModule with DbModuleService {}.f
res0: Int = 123
答案是不。 这不可能。 实际上你所说的是违背自我打字的目的。
trait DbModule {
  self: DbConfig =>
}
trait BookDbModule extends DbModule {
}
  在你的例子中(总结在这里), DbModule说我的孩子必须以某种方式提供在DbConfig定义的功能。  但是特质BookDbModule无法显示,除非它扩展DbConfig或明确地自我输入它。  这是违背你的想法... 
上一篇: type of a child trait in a parent trait in scala
下一篇: scala bind type parameter of trait with type parameter of function
