从受自我类型约束的多个特征中定义单个特征

Define a single trait out of multiple traits constrained by self-types

我有两个 abstract class,如下所示:

abstract class BddAsyncSpec extends AsyncFeatureSpec
  with Matchers
  with GivenWhenThen
  with BeforeAndAfter
  with BeforeAndAfterAll

abstract class BddSpec extends FeatureSpec
  with Matchers
  with GivenWhenThen
  with BeforeAndAfter
  with BeforeAndAfterAll

如您所见,mixin 部分看起来是一样的。我的问题是,如何抽象混合部分,当我添加更多特征时,它适用于 BddAsyncSpecBddSpec.

尝试

trait MyTraits extends Matchers
  with GivenWhenThen with BeforeAndAfter with BeforeAndAfterAll { this: Suite with Informing => }

abstract class BddAsyncSpec extends AsyncFeatureSpec with MyTraits
abstract class BddSpec extends FeatureSpec with MyTraits

我们在哪里使用 self-type

this: Suite with Informing =>

因为,例如,GivenWhenThen 需要 Informing

trait GivenWhenThen { this: Informing =>

BeforeAndAfterBeforeAndAfterAll需要Suite

trait BeforeAndAfter extends SuiteMixin { this: Suite =>

自我类型是一种指定特征需要什么才能混入的方式。

另一个特征是一组共同特征?

trait T1; trait T2
trait Common extends T1 with T2
abstract class BaseA; abstract class BaseB;
abstract class A extends BaseA with Common
abstract class B extends BaseB with Common