Is there an easy way to convert a case class into a tuple?
I can, of course, easily write boilerplate code to do this, but I mean without the boilerplate.
What I'm really after is a way to easily make a case class lexicographically Ordered. I can achieve the goal for tuples by importing scala.math.Ordering.Implicits._, and voila, my tuples have an Ordering defined for them. But the implicits in scala.math.Ordering don't work for case classes in general.
Came across this old thread while attempting to do this same thing. I eventually settled on this solution:
You might try extending the
ProductN
trait, for N=1-22, whichTupleN
extends. It will give you a lot of Tuple semantics, like the_1
,_2
, etc. methods. Depending on you how you use your types, this might be sufficient without creating an actual Tuple.Shapeless will do this for you.
Gets
productElements turns a case class into a Shapeless HList:
And HLists are converted to tuples with #tupled:
Performance is likely to be horrible, since you're constantly converting. You'd probably convert everything to the tupled form, sort that, then convert it back using something like
(Fnord.apply _).tupled
.How about calling
unapply().get()
in the companion object?