If this pattern often appears in your code, you should consider turning doSomeStuff into a Type method. Class classes in Scala are normal classes, and you should use object oriented functions when they make sense.
Otherwise, you can add a method at the top of your hierarchy, assuming that all of your case classes extend the line. For example:
class Base { def whenType(f: (T1, T2) => Unit): Unit = this match { case Type(t1, t2) => f(t1, t2) case _ => () } }
and then you can use x whenType doSomeStuff
Iulian dragos
source share