Scala 用抽象类型交换类型参数

Scala 用抽象类型交换类型参数,scala,Scala,为了避免在对参数化类进行子类化时重复类型参数信息,我尝试重写一些代码以使用抽象类型 我想表达的类似于: class Group[A] abstract class Thing[A, G <: Group[A]] { val group: G } class SomeGroup[A] extends Group[A] { g => object SomeThing extends Thing[A, SomeGroup[A]] { val group = g

为了避免在对参数化类进行子类化时重复类型参数信息,我尝试重写一些代码以使用抽象类型

我想表达的类似于:

class Group[A]

abstract class Thing[A, G <: Group[A]] {
  val group: G
}

class SomeGroup[A] extends Group[A] { g =>    
  object SomeThing extends Thing[A, SomeGroup[A]] {
    val group = g
  }
}
类组[A]
抽象类事物
对象某物扩展某物[A,某物组[A]]{
val组=g
}
}
使用抽象类型,我迄今为止的最佳尝试是:

class Group {
  type A
}

abstract class Thing { t =>
  type A
  type G <: Group { type A = t.A }
  val group: G
}

class SomeGroup extends Group { g =>
  object SomeThing extends Thing {
    type A = g.A
    type G = SomeGroup { type A = g.A }
    val group = g
  }
}
类组{
A型
}
抽象类事物{t=>
A型
G型
对象某物延伸某物{
A型=g.A型
类型G=SomeGroup{类型A=G.A}
val组=g
}
}
然而,我在最后一行得到一个编译器错误,它说“值组的类型不兼容”


如何使用抽象类型编写第一个示例?

您需要向类型检查器/推断器提供一些帮助:

val group : G = g

这比我想象的要简单。

这有什么好的理由吗?我可以想象,在没有任何帮助的情况下,类型检查器会看到
g
的类型只是
SomeGroup
,除非你使用类型注释,否则这就是分配给
group
的类型。不要相信我的话不过,这是事实。