scala - 蛋糕图案和类型

标签 scala types cake-pattern

def someA(在 trait B 中)如何使用具有相同 C#MyTypetrait A在 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")

}

它无法编译:类型不匹配;

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

最佳答案

您可以声明 doSomethingmyType 以使用 MyType 的路径独立版本,SomeType#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)
}

关于scala - 蛋糕图案和类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831329/

相关文章:

scala - 解析 xml 导致新行匹配错误

scala - 是否可以在 Haskell 中提供自定义模式分解?

ios - 一个类继承另一个类并遵守一个协议(protocol)——试图调和两个​​接口(interface)

scala - 蛋糕图案 : How to share an instance?

scala - 类标签和路径相关的类型,具有蛋糕图案的味道

json - 在 Scala 中使用 play ws POST 请求

parsing - scala 中递归解析器的高级控制

javascript - "=="的对象相等性的标准定义是什么?

reactjs - 如何在带有 typescript 的函数中接受 React.Component 参数?

scala - 避免名称与 Cake Pattern 冲突