Scala 性状参数表的改进

Scala 性状参数表的改进,scala,generics,types,traits,Scala,Generics,Types,Traits,我所看到的问题涉及到定义一个具有类型参数的特征,其中它的类型参数具有类型参数,我需要知道这些类型。例如,考虑下面的代码: trait JoinData[A如果没有关于API和如何使用API的任何细节,很难回答您的问题。目前还不清楚为什么是单态的 trait JoinData extends Something { def method1(a: Attributes): String def method2(e: Entity, r: Document): Output = {

我所看到的问题涉及到定义一个具有类型参数的特征,其中它的类型参数具有类型参数,我需要知道这些类型。例如,考虑下面的代码:


trait JoinData[A如果没有关于API和如何使用API的任何细节,很难回答您的问题。目前还不清楚为什么是单态的

trait JoinData extends Something {
  def method1(a: Attributes): String
  def method2(e: Entity, r: Document): Output = {
     ...
  } 
  ...
}
这是不够的

您可以尝试用类型成员替换(某些)类型参数:

trait Attributes
trait Entity {
  type A <: Attributes
}
trait Keyed
trait Document {
  type B <: Keyed
}
trait Something[L <: Entity, R <: Document]
trait Output[A <: Attributes, B <: Keyed]
trait JoinData[L <: Entity, R <: Document] extends Something[L, R]{
  def method1(a: L#A): String
  def method2(l: L, r: R): Output[L#A, R#B] = ???
}
trait属性
特征实体{

为没有发布让我需要这么做的原因更加明显的东西而道歉(我不得不从我正在做的事情中抽象出来,因为它有点抽象)。这个答案太完美了!我实际上尝试过路径依赖类型,但在实现中也有点愚蠢,因为在定义traits上留下了类型参数。这个答案促使我正确地完成了这项工作,并对我进行了分类。我未来的用户说谢谢!
trait Attributes
trait Entity
trait Keyed
trait Document
trait Something[L <: Entity, R <: Document]
trait Output[A <: Attributes, B <: Keyed]
trait TC[L <: Entity] {
  type A <: Attributes
}
trait TC1[R <: Document] {
  type B <: Keyed
}

abstract class JoinData[L <: Entity, R <: Document](implicit val tc: TC[L]) extends Something[L, R]{
  def method1(a: tc.A) : String
  def method2(l: L, r: R)(implicit tc1: TC1[R]): Output[tc.A, tc1.B] = ???
}