Allow unapply for any ProductN where TupleN is expected


Scala has nice shorthand syntax for unapplying tuples:

val tuple = ("hello", 42)
val (a, b) = tuple

tuple match {
  case (a, b) =>

But it works for tuples only and tuples are final case classes so it is not possible to utilize this syntax for custom data and monadic classes.


Allow such unapply for any instance implementing ProductN trait.


trait MyEither(...) //Either implementing Product2[A, B]
val (err, ok) = fun() //returns MyEither[A, B], unapplies to Option[A], Option[B]
if (err.isDefined) return Left("something happened: " + err.get) //eager return w/o building up indentation

class MyDuration //implements Product2[Long, TimeUnit]
val (num, unit) = duration


There are existing bugs on this, compiler already relies on Product instead of Tuple when compiling such code. This proposal allows to nicely close those as well.

Alternatively, why not allow pattern matching on implicit conversions? So given available implicit conversion T => (A, B) we can match T on case (a, b)

I don’t really see the problem with that notation. It seems to me to be more consistent than what you are proposing.


Allow such unapply for any instance implementing ProductN trait.

I believe case classes only extend Product, not ProductN.

True, I’m going to rewrite original post