Scala pattern matching does not support fall-throughs. I actually asked this exact question a while ago (Scala pattern matching - match multiple successful cases)
You would need to do it independently for every possible case.
If your actual use case is different (i.e. you have dozens of these conditions and you need to execute them sequentially in an elegant way) - I would possibly opt to use a different method and not pattern matching at all.
Possibly, what I would do (to maintain a functional paradigm) would be something like this:
class T
class S extends T
val myTestedVal = new S
((x:T) => (
(x match { case z:S => (()=>println(2)) case _ => } ) ::
(x match { case z:T => (()=>println(1)) case _ => } ) ::
// Add other cases here
Nil)) (myTestedVal).collect { case i:(()=>Unit) => i }.foreach(z => z())
The above sample code would print both 2 and 1 for S
, and just 1 for T
. What it does, is it basically runs the list of conditions, creating a list of functions to run based on the successful conditions, and then executes them in the foreach
(which could also be modified to be a map
if you need the functions to return meaningful values).
Note
The above method is definitely overkill for the OP's simple use-case. However, if you have multiple conditions that you want to execute in a "rule-engine" style, the above method is more elegant and "functional" than having a long chain of "if" statements.
Disclaimer: I am somewhat new to Scala and FP myself.