Scala 在函数中使用ADT的参数化分支

Scala 在函数中使用ADT的参数化分支,scala,shapeless,algebraic-data-types,Scala,Shapeless,Algebraic Data Types,代码如下: sealed trait Tr final case class A(a: String) extends Tr final case class B(b: String) extends Tr def markWithType[Type <: Tr](tr: Type): Type = tr match { //Compile error //Expression of type A doesn't conform to expected type Type c

代码如下:

sealed trait Tr
final case class A(a: String) extends Tr
final case class B(b: String) extends Tr

def markWithType[Type <: Tr](tr: Type): Type = tr match {
  //Compile error
  //Expression of type A doesn't conform to expected type Type
  case A(a) => A("A:" + a) 

  //Compile error
  //Expression of type B doesn't conform to expected type Type
  case B(b) => B("B:" + b)
}
Tr
最后一个案例类别A(A:String)扩展了Tr
最后一个案例B类(B:字符串)扩展了Tr
def标记WithType[类型B(“B:+B”)
}

问题是它无法编译。我想保留
类型,您可以使用简单的重载

sealed trait Tr {
  def markWithType: Tr
}

final case class A(a: String) extends Tr {
  override def markWithType: A = A(s"A: ${a}")
}

final case class B(b: String) extends Tr {
  override def markWithType: B = B(s"B: ${b}")
}
另一种选择是类型类,但我认为在这种情况下,这是一种过分的选择