Assuming a
should be SomeClass2
, but not SomeClass
(same with b
, c
, d
).
You can use alternative patterns such as case A | B => ...
case A | B => ...
and structural patterns, for example Some(MyClass(f))
.
You can also use the partial function in map
as map { case ... }
instead of map { x => x match {...} }
.
And I think there is an error in your code: there is a check for case Nil => ...; case SomeClass8(...) => ...
case Nil => ...; case SomeClass8(...) => ...
You can replace Seq(xs @_*)
with xs
. If you need a whole collection, you do not need to retrieve the elements.
Your code:
response match { case Left(_) | Right(SomeClass2(None)) | Right(SomeClass2(Some(SomeClass3(_, _, _, _, Nil))) => None case Right(SomeClass2(Some(SomeClass3(_, _, _, _, xs))) => xs map { case SomeClass6(None) | SomeClass6(Some(SomeClass8(Nil, _, _, _, _))) => None case SomeClass6(Some(SomeClass8(xs, _, _, _, _))) => xs map { case Nil => None case SomeClass9(g, _, _, _, _, _, _, _, _, _, _) => } } }
You should also retrieve nested matches for individual methods.
Matching patterns is not the only solution. You can use the Either
and Option
methods:
response.right.toOption.collect { // No need for the first part. case SomeClass2(Some(SomeClass3(_, _, _, _, xs)) if xs.nonEmpty => ... }