2

I would like to do a generic function able to sort different nested case classes by one of their "sub" argument in common.

For the moment I have done this:

 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)

It works well to sort a sequence of class A or class B, but I would like to make it work for a case class C or D (that contain a SortableByGeoPoint sub-class) How can I accomplish this?

I took a look at shapeless but didn't find a way to do this, but any way to do it (knowing that I will add later other classes like the class C or D), would be perfect.

1 Answer 1

4

I think you are missing some form of type constraint. The most straight forward way to make your example code compile is by doing:

case class C(a: A, other: String) extends SortableByGeoPoint {
  val geographicPoint: Int = a.geographicPoint
}
case class D(b: B, other: Int) extends SortableByGeoPoint {
  val geographicPoint: Int = b.geographicPoint
}

This might or not meet your requirements. So, let's make yet another one:

case class Z[T](b: SortableByGeoPoint, other: T)
def sortByGeoPoint2(sequence: Seq[Z]): Seq[Z] = sequence.sortBy(_.b.geographicPoint)

In fact, I can think of many other ways to achieve this same outcome. Too many to enumerate. If you can illustrate a few other requirements I might be able to find a more suitable solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.