scalatuplescase-class

In Scala, is there an easy way to convert a case class into a tuple?


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.


Solution

  • How about calling unapply().get in the companion object?

    case class Foo(foo: String, bar: Int)
    
    val (str, in) = Foo.unapply(Foo("test", 123)).get
    // str: String = test
    // in: Int = 123