如何在包装特征的自我类型中使用嵌入式类型混合特征?

时间:2015-01-24 20:06:01

标签: scala

我想知道是否可以从包装特征中混合自我类型

例如:

trait PolicyHolderDAOComponent { this: DbConfig with ActorSystemConfig =>

  //want to use the injected DbConfig and ActorSystemConfig for instantiation of policyHolderDAO 
  def policyHolderDAO = new PolicyHolderDAO 
  class PolicyHolderDAO { this: DbConfig with ActorSystemConfig => 
    ... 
  }
} 

1 个答案:

答案 0 :(得分:3)

this一个不同的名称,然后你可以从内部类引用它:

trait PolicyHolderDAOComponent { self: DbConfig with ActorSystemConfig =>
  def policyHolderDAO = new PolicyHolderDAO

  class PolicyHolderDAO { this: DbConfig with ActorSystemConfig =>
    // use self
  }
}

请注意,self的类型均为DbConfigActorSystemConfig