Is there a more readable and more reliable (for refactoring) way to match case classes like this?
Example
Hull class
A very long random class with many "fields".
case class Data(name: String, time: Long, ..., userId: Option[UUID] ..., orders: Int, ... )
Matching Pattern: Option a
Works. But an error occurred while changing the position of the field. One ends with a count of _ .
res match { case data@Data (_,_,_,_,_,_,Some(id),_,_,_,6,_,_) => (id, data.orders) case _ => ... }
Matching Pattern: Option B
It also works. It is stable for changing orders. It turns out very cumbersome, with a lot of checks in the guard. It is also necessary to repeat the reading of the value.
res match { case data: Data if data.userId.isDefined && data.orders == 6 => (data.userId.get,data.orders) case _ => ... }
Rephrased question
Is there a way to combine Options A and B to benefit from both approaches?
source share