Scala pattern matching @

A little more about pattern matching !

And we are going to learn about a new operator: @ !

The operator @ allows you to capture the full entity the pattern matching has matched on.

It is convenient in numerous use case but here is a few examples.

case a @ "something" => // something with a

It is also very convenient in cases with Option:

opt match {
    case s @ Some("abc") => s
    case _ => Some(default)
}

Without it you would have to recreate a new object instead of reusing the one we already have.

Reveal more information and clues
Load Exercise