2
我想做一個泛型函數能夠通過其中一個「子」參數共同排序不同的嵌套案例類。斯卡拉:泛型函數與嵌套案例類
目前我已經做到了這一點:
sealed trait SortableByGeoPoint {
val geographicPoint: Int
}
case class A(id: Int, geographicPoint: Int) extends SortableByGeoPoint
case class B(image: String, geographicPoint: Int) extends SortableByGeoPoint
case class C(a: A, other: String)
case class D(b: B, other: Int)
def sortByGeoPoint(sequence: Seq[SortableByGeoPoint]): Seq[SortableByGeoPoint] = sequence.sortBy(_.geographicPoint)
它運作良好排序的A類或B類的順序,但我想使它成爲一個案例類C或d工作(包含一個SortableByGeoPoint子類)我該如何做到這一點?
我看了看不成形,但沒有找到一個方法來做到這一點,但沒有辦法做到這一點(知道我會添加像C級或d後其他類),將是完美的。