这个问题与this类似,只是两个案例类实例都需要通过引用它们的基本特征来访问。这样做的原因是多个案例类将扩展特征,并且直到运行时才会知道确切的类型:
sealed trait baseData {
def weight: Int
def priority: Int
}
sealed trait moreData {
def weight: Int
def priority: Int
def t: String
def id: String
}
case class data1(override val weight: Int, override val priority: Int) extends baseData
case class moreData1 (override val weight:Int, override val priority: Int, override val t: String, override val id: String)extends moreData
val from: baseData = data1(1,2)
val to: moreData = moreData1(3,4,"a","b")
如何编写具有以下签名的函数,将from
复制到to
?
def copyOver[A <:baseData, B <:moreData](from: A, to: B)
我确信这对Shapeless是可行的,但我遇到了麻烦,因为我很陌生。