Is there a full specification for pattern matching possibilities of Scala?
I am unable to fix following code:
something match {
case e @ (sae: ServerApiException if sae.statusCode == 401 | _: UnauthorizedException) => {
doSomething(e)
}
...
}
(It does not compile in 2.8.1.)
I'm not sure I'd write the code this way; it's hard to follow (in addition to not working in its original form).
I'd rather go for something like
def doSomething(e: Exception) = { /* whatever */ }
something match {
case sae: ServerApiException if (sae.statusCode == 401) => doSomething(sae)
case ue: UnauthorizedException => doSomething(ue)
}
to avoid duplicate code. Or you could use options:
(something match {
case sae: ServerApiException if (sae.statusCode == 401) => Some(sae)
case ue: UnauthorizedException => Some(ue)
case _ => None
}).foreach(e => /* do something */ )
if you prefer to write the method afterwards. But I think the first way is likely the clearest.
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