Scala 编译器是否有可能理解重写的抽象类型

Is it possible for the Scala compiler to understand overridden Abstract Types

上下文:

我正在尝试在 RESTful 端点中使用 Jackson JsonSubTypes 提供的多态子类型。各种请求体之间有一些共性,所以我有:

abstract class A extends Translatable {
  val infoA: String
  val infoB: String
}

case class B(
  infoA: String,
  infoB: String,
  infoC: String,
  infoD: Int
) extends A {
  override type T = B
  def translate(
    requestInfo: String,
    request: B
  ): String = s"<$infoA,$infoB>:$infoC-${infoD.toString}"
}


case class C(
  infoA: String,
  infoB: String,
  infoC: Array[String]
) extends A {
  override type T = C
  def translate(
    requestInfo: String,
    request: C
  ): String = s"<$infoA,$infoB>:${infoC.mkString}"
}

具有以下特征,因此无需将传入的 A 请求与 BC 进行模式匹配:

trait Translatable {
  type T <: A
  def translate(
    requestInfo: String,
    request: T
  ): String
}

问题是当我编译它时抱怨类型不匹配:

found: request.type (with underlying type com.program.A)
required: request.T

我尝试了一些方法,但都无济于事,只有这个似乎很接近:

e.g.,
def translate[X](
  requestInfo: String,
  request: X
)(implicit ev: X <:< A) = ...
(modifying the case class impl. of ^ accordingly)

但这会导致

Cannot prove that com.program.A <:< request.T

关于如何做到这一点有什么想法吗?还是我必须忍受模式匹配?我说是因为第一个代码块工作当且仅当我模式匹配并调用具体实现。 BCtranslate,但我更愿意只使用 A 类型参数并对其调用 translate。这可能吗?

编辑:添加上述产生编译错误的实例:

class Program {
  def doWork(
    info: String,
    request: A
  ): String = {
    request.translate(
      info,
      request // compilation fails/complains about this param in both cases
    )
  }
}

EDIT2:添加限定条件:我需要在子类型 B & C 中覆盖类型,以便在翻译函数中。我可以调用特定于 BC

的字段

尝试添加细化上限的类型参数

def doWork[T0 <: A { type T = T0 }](
            info: String,
            request: T0
          ): String = {
  request.translate(
    info,
    request
  )
}

doWork("info1", B("infoA1", "infoB1", "infoC1", 1))//<infoA1,infoB1>:infoC1-1
doWork("info2", C("infoA2", "infoB2", Array("infoC21", "infoC22")))//<infoA2,infoB2>:infoC21infoC22