蛋糕模式和类型

jwi*_*ndy 5 types scala cake-pattern

如何def someA(in trait B)使用trait AC#MyTypein中相同B?(然后A#MyType =:= B#MyType)

trait C {
  type MyType
}


trait A {
  self: C =>
  def doSomething(s: MyType) { println(s.toString)}
}

trait B {
  self: C =>  

  def someA: A
  def myType: MyType

  def action = someA.doSomething(myType)
}

// Mix part

case class Ahoy(value: String)

trait ConcreteC extends C {  
  type MyType = Ahoy
}


class PieceOfCake extends B with ConcreteC {
  val someA = new A with ConcreteC
  val myType = Ahoy("MyType")

}
Run Code Online (Sandbox Code Playgroud)

它不编译:类型不匹配;

[error]  found   : B.this.MyType
[error]  required: _1.MyType where val _1: A
[error]   def action = someA.doSomething(myType))
Run Code Online (Sandbox Code Playgroud)

Mat*_*ell 3

您可以声明doSomething并使用,myType的路径独立版本:MyTypeSomeType#MyType

trait SomeType {
  type MyType
}


trait A {
  self: SomeType =>
  def doSomething(s: SomeType#MyType) { println(s.toString)}
}

trait B {
  self: SomeType =>  

  def someA: A
  def myType: SomeType#MyType

  def action = someA.doSomething(myType)
}
Run Code Online (Sandbox Code Playgroud)