Compact pattern match (1 line?)

trait HasAfterUpdate{
def afterUpdate(): Unit
}

----- now -------
somethig match {
case au: HasAfterUpdate => au.afterUpdate()
case _ =>
}

Question: is there any compact way to do, 1 line, like this or somthing…
this match (_: HasAfterUpdate => _.afterUpdate())

Tip: Surround code in triple backticks for better formatting.

You can put multiple case statements on one line. There’s no special syntax or formatting needed.

scala> 0 match { case 0 => "hi" case 3 => "bye" }
res0: String = hi

Some(something).collect { case hau: HasAfterUpdate => hau.afterUpdate }

But I think your code is better (with minor correction), since it makes the intention much clearer. It can be written in one line, if your line is long enough. :slight_smile:

something match { case au: HasAfterUpdate => au.afterUpdate(); case _ => () }

1 Like