In my test suite I am frequently writing code such as
val Vector(socialSci, humanities, pureSci, pureApplSci) = foo(someValue)
foo
obviously returns a (short) Vector but I'd like to have names for each of the values. It's not always four items long, but since it's my test suite the correct number is known and I'm perfectly fine with an exception being thrown if there is a length mismatch.
The above code works -- except that I get a compiler error:
match may not be exhaustive.
Is there a way to get rid of the compiler warning? I've tried @nowarn
and @nocheck
in various positions with no luck.
Alternatively, is there a succinct way to write this so it doesn't cause a warning in the first place? The solution at Convert Vector to Tuple scala looks promising except that reflection isn't available for ScalaJS.
def toTuple(seq: Seq[_]): Product = {
val clz = Class.forName("scala.Tuple" + seq.size)
clz.getConstructors()(0).newInstance(seq.map(_.asInstanceOf[AnyRef]): _*).asInstanceOf[Product]
}
Is there an approach using a builder or something?
Update:
Thanks to Dmytro Mitin for reminding me to check my compiler options. Yes, I had completely forgotten about that and had -Xlint:valpattern
enabled. Turning that off removes the warning.
That solves my immediate problem. But as a challenge, is there a way to implement toTuple
without using reflection?