Is there a more readable and and more robust (to refactoring) way to match on case classes like this ?
Very long case class with many "fields".
case class Data(name: String, time: Long, ..., userId: Option[UUID] ..., orders: Int, ... ) //more fields fields more
Works. But error prone when fields position changes. One ends up counting _
.
res match {
case data@Data(_,_,_,_,_,_,Some(id),_,_,_,6,_,_) => (id, data.orders)
case _ => ...
}
Works also. Is stable to changing orders. Gets really cumbersome with more checks in the guard. Also reading the value has to be repeated.
res match {
case data: Data if data.userId.isDefined && data.orders == 6 => (data.userId.get,data.orders)
case _ => ...
}
Is there a way to combine Variant A and B to get the benefit of both approaches ?
The match method takes a number of cases as an argument. Each alternative takes a pattern and one or more expressions that will be performed if the pattern matches.
Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C.
Pattern Matching works by "reading" through text strings to match patterns that are defined using Pattern Matching Expressions, also known as Regular Expressions. Pattern Matching can be used in Identification as well as in Pre-Classification Processing, Page Processing, or Storage Processing.
A Case Class or Case Object can extend a Trait. Create a trait using trait keyword which contains methods you want the inherited classes to implement.
You can use a custom extractor:
res match {
case ExtractUserIdAndOrders(Some(id), 6) => ...
case _ => ...
}
where
object ExtractUserIdAndOrders {
def unapply(data: Data) = Some((data.userId, data.orders))
}
You can define it inside the method if you need it just once, or in a wider scope for multiple similar matches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With