【发布时间】:2020-06-18 23:06:10
【问题描述】:
代码如下:
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)
}
问题是它无法编译。我想保留Type <: Tr 并使其编译成功。有没有办法做到这一点?
我很确定 shapeless 在这里会有所帮助。
【问题讨论】:
标签: scala shapeless algebraic-data-types