Scala 提取嵌套类型而不传递两次?

Scala 提取嵌套类型而不传递两次?,scala,Scala,这只是一个基本的例子,但是否可以重写最后一个特征,以避免使用2类型(T,U)设置,从而通过使用M来提取或推断T?我更多的是询问没有显式对象的类型系统,但也许这是唯一的方法 trait Data[T]{ val x:T } trait Meta[T<:Data[T]]{ val m:T } trait Overall[T, M<:Meta[T] ] { def review(t:T): M } //assume we want to work with the nest

这只是一个基本的例子,但是否可以重写最后一个特征,以避免使用2类型(T,U)设置,从而通过使用M来提取或推断T?我更多的是询问没有显式对象的类型系统,但也许这是唯一的方法

trait Data[T]{
  val x:T
}

trait Meta[T<:Data[T]]{
  val m:T
}

trait Overall[T, M<:Meta[T] ] {
 def review(t:T): M
}

//assume we want to work with the nested data type Int
case class Helper extends Overall[Int,Meta[Int]]
trait数据[T]{
val x:T
}

trait-Meta[T我不认为您可以像您希望的那样直接派生类型。但是,您可以通过将类型改为类型变量来隐藏签名中的类型

trait Overall[T] {
  type M <: Meta[T]
  def review(t:T): M
}

case class Helper extends Overall[Int] {
  override type M = Meta[Int]
  def review(t: Int): Meta[Int] = null;
}
trait总体[T]{

键入M或将类型成员用于
Meta

trait Meta {
  type T <: Data[T] 
  val m: T
}

trait Overall[M <: Meta] {
  def review(t: M#T): M
}

case class IntData(x: Int) extends Data[Int]

case class Helper extends Overall[Meta { type T = IntData }] {
  def review(t: IntData) = ???
}

也许这不是你想要的。

是的,我想我需要回到最初的原因,我需要做这件事,但是这给了我很多考虑。
trait Meta {
  type T <: Data[T] 
  val m: T
}

trait Overall[M <: Meta] {
  def review(t: M#T): M
}

case class IntData(x: Int) extends Data[Int]

case class Helper extends Overall[Meta { type T = IntData }] {
  def review(t: IntData) = ???
}
case class Helper extends Overall[Meta { type T = IntData }] {
  def review(t: IntData) = new Meta {
    type T = IntData
    val m = IntData(1234)
  }
}

<console>:14: error: overriding type T in trait Meta with bounds <: Data[this.T];
 type T has incompatible type
           type T = IntData
                ^
case class IntData() extends Data[IntData] { val x = this } // !?!

case class Helper extends Overall[Meta { type T = IntData }] {
  def review(t: IntData) = new Meta {
    type T = IntData
    val m = IntData()
  }
}